/* **************************************************************************/
/*COPYRIGHT CCWIN-IN ALL RIGHTS RESERVED. THIS IS AN UNPUBLISHED WORK.      */
/*V8:ConvertMode=Report                                                     */
/*V8:WebEnabled=Yes                                                         */
/*V8:RunMode=Character,Windows                                              */
/*EXTRACTING DATA FROM MFG/PRO                                              */
/****************************************************************************/
/* PROCEDURE NAME        : xxcstallmain_e .p                                */
/* PROCEDURE TYPE        : Variance allocation program                      */
/* DESCRIPTION           : Variance allocation program                      */
/*                                                                          */     
/* INCLUDE FILES         :                                                  */
/* CALLED BY             :                                                  */
/* CALLED PROCEDURES     :                                                  */
/****************************************************************************/
/* CREATED BY            : Gaoyan                               22-Aug-19   */
/****************************************************************************/ 
/****************************************************************************/
/****************************************************************************/
											 
using com.qad.eefin.buser.EntitySecurityCheck.

&scoped-define TimeStamp STRING(MONTH(TODAY),"99") + STRING(DAY(TODAY),"99") +~
                         STRING(YEAR(TODAY),"9999") +~
                         SUBSTRING(STRING(TIME,"HH:MM:SS"),1,2) +~
                         SUBSTRING(STRING(TIME,"HH:MM:SS"),4,2) +~
                         SUBSTRING(STRING(TIME,"HH:MM:SS"),7,2)
                         
{us/mf/mfdtitle.i}
{us/gp/gpglefdf.i }						

/* Variable definitions */
DEFINE VARIABLE entity              LIKE  en_entity   FORMAT "x(8)"     NO-UNDO.
DEFINE VARIABLE site                LIKE  si_site                       NO-UNDO.
DEFINE VARIABLE li_year             LIKE  glc_year                      NO-UNDO.
DEFINE VARIABLE li_period           LIKE  glc_per                       NO-UNDO.
DEFINE VARIABLE lastyear             LIKE  glc_year                      NO-UNDO.
DEFINE VARIABLE lastperiod           LIKE  glc_per                       NO-UNDO.
DEFINE VARIABLE li_cur_yr           LIKE  glc_year                      NO-UNDO.
DEFINE VARIABLE li_cur_per          LIKE  glc_per                       NO-UNDO.
DEFINE VARIABLE ld_start_dt         LIKE  glc_start                     NO-UNDO.
DEFINE VARIABLE ld_end_dt           LIKE  glc_end                       NO-UNDO.
DEFINE VARIABLE li_access           AS    INTEGER                       NO-UNDO.
DEFINE VARIABLE lc_tmpstr           AS    CHARACTER                     NO-UNDO.
DEFINE VARIABLE li_success          AS    LOGICAL                       NO-UNDO.
DEFINE VARIABLE lc_tmpyn            AS    LOGICAL                       NO-UNDO.
DEFINE VARIABLE lc_log_file         AS    CHARACTER   FORMAT "x(40)"    NO-UNDO. 
DEFINE VARIABLE lc_to_code          AS    CHARACTER   INIT "UTF-8"      NO-UNDO. 
DEFINE VARIABLE li_update           as   logic                          no-undo.

/* For calling QAD Financials Data Object to check GL Entity Security */
DEFINE VARIABLE hQADFinancialsLib   AS    HANDLE                        NO-UNDO.
DEFINE VARIABLE EntitySecurity      AS    EntitySecurityCheck           NO-UNDO.
DEFINE VARIABLE valid_entity        LIKE  mfc_logical                   NO-UNDO.
DEFINE VARIABLE BufErrorMessage     AS    CHARACTER                     NO-UNDO.
DEFINE VARIABLE ErrorMessage        AS    CHARACTER                     NO-UNDO.

DEFINE VARIABLE totBeginvqty        like  ld_qty_oh                     no-undo.
DEFINE VARIABLE totRctinvqty        like  ld_qty_oh                     no-undo.
DEFINE VARIABLE totrqty        like  ld_qty_oh                     no-undo.
define VARIABLE debug-prog         as  logical                        no-undo.


define temp-table tt_xxcstallhist like xxcstall_hist.
						  

	
FORM
    entity     COLON 20 
    site       COLON 20 
    li_year    COLON 20 
    li_period  COLON 20 
	li_update  colon 20
    WITH FRAME a WIDTH 80 SIDE-LABELS .
    setFrameLabels(FRAME a:HANDLE).

/* Main Loop starts */
mainloop:
REPEAT TRANSACTION ON ERROR UNDO mainloop, RETRY mainloop:
   
   ASSIGN entity           = ""
          site             = ""
          li_year          = YEAR(TODAY)
          li_period        = MONTH(TODAY)
          lc_log_file      = "history_load_" +
                             {&TimeStamp} + ".txt"
          .
   
   
   FIND FIRST glc_cal NO-LOCK WHERE glc_domain = global_domain
                                AND glc_start <= TODAY
                                AND glc_end   >= TODAY NO-ERROR.
   IF AVAILABLE glc_cal THEN 
      ASSIGN li_cur_yr  = glc_year 
             li_cur_per = glc_per
             li_year    = glc_year
             li_period  = glc_per.
   
   CLEAR FRAME a NO-PAUSE.
   
   UPDATE entity   
          WITH FRAME a EDITING:
          
      {us/mf/mfnp05.i en_mstr en_entity "en_domain = global_domain" 
                      en_entity "input entity "}
      IF recno <> ? THEN DO:
         ASSIGN entity = en_entity.
         DISPLAY
            entity
         WITH FRAME a.
      END.         
   END. /* UPDATE EDITING entity */
   
   
   IF NOT CAN-FIND( FIRST en_mstr WHERE en_domain = global_domain 
                                   AND en_entity = entity)
   THEN DO:
      /* Invalid entity */
      {us/bbi/pxmsg.i &MSGNUM=3061 &ERRORLEVEL=3}
      NEXT-PROMPT entity WITH FRAME a.
      UNDO, RETRY.
   END. /* IF NOT CAN-FIND(FIRST */
      
   /* CHECK ENTITY SECURITY */
   IF entity <> "" THEN DO:    
      {us/gl/glenchk.i &entity=entity &entity_ok=valid_entity} 
      IF NOT valid_entity THEN DO:
         NEXT-PROMPT entity WITH FRAME a.
         UNDO, RETRY.
      END.     
   END. /* IF entity <> "" */
   
   siloop:
   DO ON ERROR UNDO siloop, RETRY siloop:
      
      UPDATE site     
           WITH FRAME a EDITING:
         
         {us/mf/mfnp05.i si_mstr si_site "si_domain = global_domain" 
                            si_site "input site"}
            IF recno <> ? THEN DO:
               ASSIGN site = si_site.
               DISPLAY site WITH FRAME a.
            END.            
      END. /* UPDATE EDITING site */
      
      IF NOT CAN-FIND( FIRST si_mstr WHERE si_domain = global_domain 
                                      AND si_site   = site
									  AND si_entity = entity) 
      THEN DO:
         /* Invalid Site */
         {us/bbi/pxmsg.i &msgnum=2797 &errorlevel=3 }
         NEXT-PROMPT site WITH FRAME a.
         UNDO siloop, RETRY siloop.
      END.

      /* Site Security Check */
      {us/bbi/gprun.i ""gpsiver.p"" "(INPUT site,?,OUTPUT li_access)"}
      IF li_access = 0 THEN DO:
         /* User does not have access to this site */
         {us/bbi/pxmsg.i &MSGNUM=725 &ERRORLEVEL=3}
         NEXT-PROMPT site.
         UNDO siloop, RETRY siloop.
      END. /* IF li_access = 0 */      
   END. /* siloop */
   
   yrloop:
   DO ON ERROR UNDO yrloop, RETRY yrloop:
   
         UPDATE li_year  
             li_period WITH FRAME a.
       /*      
      IF LENGTH(li_year) <> 4 THEN DO:
         {us/bbi/pxmsg.i &msgnum=3900 &errorlevel=1 &msgarg1=""Year"" }
         NEXT-PROMPT li_year.
         UNDO yrloop, RETRY yrloop.
      END.
            
      IF li_period < 1 OR li_period > 12 THEN DO:
         {us/bbi/pxmsg.i &msgnum=282 &errorlevel=1 &msgarg1=""Period"" }
         NEXT-PROMPT li_period.
         UNDO yrloop, RETRY yrloop.
      END.
      */
   END. /* yrloop */
   
   Update li_update WITH FRAME a.
   
   empty temp-table tt_xxcstallhist.

    for first xxcstctrl_ctrl where xxcstctrl_ctrl.xxcstctrl_domain = global_domain 
                           and xxcstctrl_ctrl.xxcstctrl_entity = entity 
			               and xxcstctrl_ctrl.xxcstctrl_site = site 
                           and xxcstctrl_ctrl.xxcstctrl_ID  = ""  
						   and xxcstctrl_ctrl.xxcstctrl_year = li_year
						   and xxcstctrl_ctrl.xxcstctrl_per = li_period:
    end. /*for first xxcstctrl_ctrl.xxcstctrl_file*/
    if not available xxcstctrl_ctrl then do:
	
	{us/bbi/pxmsg.i &MSGNUM=4400  &ERRORLEVEL=4 &PAUSEAFTER }
	
	Leave.
	end.
	
	if available xxcstctrl_ctrl then do:
	
     /**   Step 1 Get Begin and Current Variance amount **/ 
	 /**   Step 2 Get Total Receipt Qty     **/  
	 /**   Step 3 Get Begin Inventory Qty  ***/
		  
     /**   Step 4 Calculate Allocation Rate ***/
     /**   Step 5 Calculate Allocateion amount ***/
     /**   Step 6 Calculate WO rollup variance ***/
     /**   Step 7 Output Result **/	 
	 
	/** step 1.1 copy last month end balance to current month begin balance */ 
	if li_period - 1 = 0 then 
    assign
    lastyear   = li_year - 1	
	lastperiod = 12.
	else 
	assign
	lastperiod = li_period - 1
	lastyear   = li_year.
	
    for each xxcstall_hist where xxcstall_hist.xxcstall_domain       = global_domain
   	                   and   xxcstall_hist.xxcstall_site    =  site 
					   and   xxcstall_hist.xxcstall_year    =  lastyear
					   and   xxcstall_hist.xxcstall_period  =  lastperiod
   				       and   xxcstall_hist.xxcstall_entity  =  entity
					   no-lock.
		if debug-prog = yes then
		status default xxcstall_hist.xxcstall_part.
		create tt_xxcstallhist.
		assign tt_xxcstallhist.xxcstall_year                    = li_year
               tt_xxcstallhist.xxcstall_period                  = li_period
               tt_xxcstallhist.xxcstall_part                    = xxcstall_hist.xxcstall_part
               tt_xxcstallhist.xxcstall_site                    = xxcstall_hist.xxcstall_site
               tt_xxcstallhist.xxcstall_prodline                = xxcstall_hist.xxcstall_prodline
               tt_xxcstallhist.xxcstall_date                    = today
               tt_xxcstallhist.xxcstall_time                    = time
               tt_xxcstallhist.xxcstall_userid                  = global_userid
               tt_xxcstallhist.xxcstall_domain                  = xxcstall_hist.xxcstall_domain
               tt_xxcstallhist.xxcstall_entity                  = xxcstall_hist.xxcstall_entity
			   tt_xxcstallhist.xxcstall_begppv                  = xxcstall_hist.xxcstall_endppv
               tt_xxcstallhist.xxcstall_begfreight              = xxcstall_hist.xxcstall_endfreight
               tt_xxcstallhist.xxcstall_begapusage              = xxcstall_hist.xxcstall_endapusage
               tt_xxcstallhist.xxcstall_begaprate               = xxcstall_hist.xxcstall_endaprate
               tt_xxcstallhist.xxcstall_begcstadj               = xxcstall_hist.xxcstall_endcstadj
               tt_xxcstallhist.xxcstall_begcstadj_mtl           = xxcstall_hist.xxcstall_endcstadj_mtl
               tt_xxcstallhist.xxcstall_begcstadj_lbr           = xxcstall_hist.xxcstall_endcstadj_lbr
               tt_xxcstallhist.xxcstall_begcstadj_bdn           = xxcstall_hist.xxcstall_endcstadj_bdn
               tt_xxcstallhist.xxcstall_begcstadj_ovh           = xxcstall_hist.xxcstall_endcstadj_ovh
               tt_xxcstallhist.xxcstall_begcstadj_sub           = xxcstall_hist.xxcstall_endcstadj_sub
               tt_xxcstallhist.xxcstall_begcyccnt               = xxcstall_hist.xxcstall_endcyccnt
               tt_xxcstallhist.xxcstall_begcyccnt_mtl           = xxcstall_hist.xxcstall_endcyccnt_mtl
               tt_xxcstallhist.xxcstall_begcyccnt_lbr           = xxcstall_hist.xxcstall_endcyccnt_lbr
               tt_xxcstallhist.xxcstall_begcyccnt_bdn           = xxcstall_hist.xxcstall_endcyccnt_bdn
               tt_xxcstallhist.xxcstall_begcyccnt_ovh           = xxcstall_hist.xxcstall_endcyccnt_ovh
               tt_xxcstallhist.xxcstall_begcyccnt_sub           = xxcstall_hist.xxcstall_endcyccnt_sub
               tt_xxcstallhist.xxcstall_begcommon               = xxcstall_hist.xxcstall_endcommon
               tt_xxcstallhist.xxcstall_begothers               = xxcstall_hist.xxcstall_endothers
               tt_xxcstallhist.xxcstall_sbegmusage              = xxcstall_hist.xxcstall_sendmusage
               tt_xxcstallhist.xxcstall_sbegmrate               = xxcstall_hist.xxcstall_sendmrate
               tt_xxcstallhist.xxcstall_sbegmmthd               = xxcstall_hist.xxcstall_sendmmthd
               tt_xxcstallhist.xxcstall_sbegmmthd_mtl           = xxcstall_hist.xxcstall_sendmusage_mtl
               tt_xxcstallhist.xxcstall_sbegmmthd_lbr           = xxcstall_hist.xxcstall_sendmusage_lbr
               tt_xxcstallhist.xxcstall_sbegmmthd_bdn           = xxcstall_hist.xxcstall_sendmusage_bdn
               tt_xxcstallhist.xxcstall_sbegmmthd_ovh           = xxcstall_hist.xxcstall_sendmusage_ovh
               tt_xxcstallhist.xxcstall_sbegmmthd_sub           = xxcstall_hist.xxcstall_sendmusage_sub
               tt_xxcstallhist.xxcstall_sbegsubusage            = xxcstall_hist.xxcstall_sendsubusage
               tt_xxcstallhist.xxcstall_sbegsubusage_mtl        = xxcstall_hist.xxcstall_sendmmthd_mtl
               tt_xxcstallhist.xxcstall_sbegsubusage_lbr        = xxcstall_hist.xxcstall_sendmmthd_lbr
               tt_xxcstallhist.xxcstall_sbegsubusage_bdn        = xxcstall_hist.xxcstall_sendmmthd_bdn
               tt_xxcstallhist.xxcstall_sbegsubusage_ovh        = xxcstall_hist.xxcstall_sendmmthd_ovh
               tt_xxcstallhist.xxcstall_sbegsubusage_sub        = xxcstall_hist.xxcstall_sendmmthd_sub
               tt_xxcstallhist.xxcstall_sbegsubrate             = xxcstall_hist.xxcstall_sendsubrate
               tt_xxcstallhist.xxcstall_sbeglbrusage            = xxcstall_hist.xxcstall_sendlbrusage
               tt_xxcstallhist.xxcstall_sbeglbrrate             = xxcstall_hist.xxcstall_sendlbrrate
               tt_xxcstallhist.xxcstall_sbegbdnusage            = xxcstall_hist.xxcstall_sendbdnusage
               tt_xxcstallhist.xxcstall_sbegbdnrate             = xxcstall_hist.xxcstall_sendbdnrate
               tt_xxcstallhist.xxcstall_sbegcstadj              = xxcstall_hist.xxcstall_sendcstadj
               tt_xxcstallhist.xxcstall_sbegcstadj_mtl          = xxcstall_hist.xxcstall_sendcstadj_mtl
               tt_xxcstallhist.xxcstall_sbegcstadj_lbr          = xxcstall_hist.xxcstall_sendcstadj_lbr
               tt_xxcstallhist.xxcstall_sbegcstadj_bdn          = xxcstall_hist.xxcstall_sendcstadj_bdn
               tt_xxcstallhist.xxcstall_sbegcstadj_ovh          = xxcstall_hist.xxcstall_sendcstadj_ovh
               tt_xxcstallhist.xxcstall_sbegcstadj_sub          = xxcstall_hist.xxcstall_sendcstadj_sub
               tt_xxcstallhist.xxcstall_sbegcyccnt              = xxcstall_hist.xxcstall_sendcyccnt
               tt_xxcstallhist.xxcstall_sbegcyccnt_mtl          = xxcstall_hist.xxcstall_sendcyccnt_mtl
               tt_xxcstallhist.xxcstall_sbegcyccnt_lbr          = xxcstall_hist.xxcstall_sendcyccnt_lbr
               tt_xxcstallhist.xxcstall_sbegcyccnt_bdn          = xxcstall_hist.xxcstall_sendcyccnt_bdn
               tt_xxcstallhist.xxcstall_sbegcyccnt_ovh          = xxcstall_hist.xxcstall_sendcyccnt_ovh
               tt_xxcstallhist.xxcstall_sbegcyccnt_sub          = xxcstall_hist.xxcstall_sendcyccnt_sub
               tt_xxcstallhist.xxcstall_sbegcommon              = xxcstall_hist.xxcstall_sendcommon
               tt_xxcstallhist.xxcstall_sbegothers              = xxcstall_hist.xxcstall_sendothers
               tt_xxcstallhist.xxcstall_sbegraw                 = xxcstall_hist.xxcstall_sendraw
               tt_xxcstallhist.xxcstall_sbegsf                  = xxcstall_hist.xxcstall_sendsf
               tt_xxcstallhist.xxcstall_sbegsf_mtl              = xxcstall_hist.xxcstall_sendsf_mtl
               tt_xxcstallhist.xxcstall_sbegsf_lbr              = xxcstall_hist.xxcstall_sendsf_lbr
               tt_xxcstallhist.xxcstall_sbegsf_bdn              = xxcstall_hist.xxcstall_sendsf_bdn
               tt_xxcstallhist.xxcstall_sbegsf_ovh              = xxcstall_hist.xxcstall_sendsf_ovh
               tt_xxcstallhist.xxcstall_sbegsf_sub              = xxcstall_hist.xxcstall_sendsf_sub
		.
	end. /** end of copy last month end balance to current month begin balance */
	 
	/* 1.2 copy current month variance to tt_xxcstallhist */ 
     for each xxcsttrvar_mstr no-lock where xxcsttrvar_mstr.xxcsttrvar_domain = global_domain
                                        and xxcsttrvar_mstr.xxcsttrvar_site   =  site
 								        and xxcsttrvar_mstr.xxcsttrvar_year   =  li_year
 								        and xxcsttrvar_mstr.xxcsttrvar_period =  li_period
 								        and xxcsttrvar_mstr.xxcsttrvar_entity =  entity
										.
		    find pt_mstr where pt_part = xxcsttrvar_mstr.xxcsttrvar_part
                           and pt_domain = global_domain no-lock no-error.
            find ptp_det where ptp_domain = global_domain
                            and ptp_part   = xxcsttrvar_mstr.xxcsttrvar_part
							and ptp_site   = site
                            no-lock no-error.
			if debug-prog = yes then
         status default xxcsttrvar_mstr.xxcsttrvar_part + " " + xxcsttrvar_mstr.xxcsttrvar_var_type	.				
			find tt_xxcstallhist where tt_xxcstallhist.xxcstall_domain  = global_domain
			                       and tt_xxcstallhist.xxcstall_entity  = entity
			                       and tt_xxcstallhist.xxcstall_year    = li_year  
	                               and tt_xxcstallhist.xxcstall_period  = li_period
	                               and tt_xxcstallhist.xxcstall_part    = xxcsttrvar_mstr.xxcsttrvar_part
	                               and tt_xxcstallhist.xxcstall_site    = site
								   no-error.
								  
	   if not available tt_xxcstallhist then do:
		create tt_xxcstallhist.
		assign tt_xxcstallhist.xxcstall_domain            = global_domain
		       tt_xxcstallhist.xxcstall_year              = li_year  
               tt_xxcstallhist.xxcstall_period            = li_period
               tt_xxcstallhist.xxcstall_part              = xxcsttrvar_mstr.xxcsttrvar_part
               tt_xxcstallhist.xxcstall_site              = site
               tt_xxcstallhist.xxcstall_prodline          = pt_mstr.pt_prod_line when available pt_mstr
               tt_xxcstallhist.xxcstall_date              = today
               tt_xxcstallhist.xxcstall_time              = time
               tt_xxcstallhist.xxcstall_userid            = global_userid
               tt_xxcstallhist.xxcstall_entity            = entity.
		end.
		/*
        if xxcsttrvar_mstr.xxcsttrvar_part = "37150-F9100" then message xxcsttrvar_mstr.xxcsttrvar_amount view-as alert-box.
		*/
		assign
			   tt_xxcstallhist.xxcstall_curppv            = xxcsttrvar_mstr.xxcsttrvar_amount  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "PPV"
               tt_xxcstallhist.xxcstall_curfreight        = xxcsttrvar_mstr.xxcsttrvar_amount  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "FREIGHT"
               tt_xxcstallhist.xxcstall_curapusage        = xxcsttrvar_mstr.xxcsttrvar_amount  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "APUSAGE"
               tt_xxcstallhist.xxcstall_curaprate         = xxcsttrvar_mstr.xxcsttrvar_amount 
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "APRATE"
               tt_xxcstallhist.xxcstall_curcstadj         = xxcsttrvar_mstr.xxcsttrvar_amount  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "CSTADJ" 
															and (available ptp_det and ptp_det.ptp_pm_code = "P"
															or (not available ptp_det and available pt_mstr and pt_mstr.pt_pm_code = "P"))
               tt_xxcstallhist.xxcstall_curcstadj_mtl     = xxcsttrvar_mstr.xxcsttrvar_mtl_std 
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "CSTADJ" 
															and (available ptp_det and ptp_det.ptp_pm_code = "P"
															or (not available ptp_det and available pt_mstr and pt_mstr.pt_pm_code = "P"))
               tt_xxcstallhist.xxcstall_curcstadj_lbr     = xxcsttrvar_mstr.xxcsttrvar_lbr_std 
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "CSTADJ" 
															and (available ptp_det and ptp_det.ptp_pm_code = "P"
															or (not available ptp_det and available pt_mstr and pt_mstr.pt_pm_code = "P"))
               tt_xxcstallhist.xxcstall_curcstadj_bdn     = xxcsttrvar_mstr.xxcsttrvar_bdn_std 
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "CSTADJ" 
															and (available ptp_det and ptp_det.ptp_pm_code = "P"
															or (not available ptp_det and available pt_mstr and pt_mstr.pt_pm_code = "P"))
               tt_xxcstallhist.xxcstall_curcstadj_ovh     = xxcsttrvar_mstr.xxcsttrvar_ovh_std 
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "CSTADJ" 
															and (available ptp_det and ptp_det.ptp_pm_code = "P"
															or (not available ptp_det and available pt_mstr and pt_mstr.pt_pm_code = "P"))
               tt_xxcstallhist.xxcstall_curcstadj_sub     = xxcsttrvar_mstr.xxcsttrvar_sub_std 
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "CSTADJ" 
															and (available ptp_det and ptp_det.ptp_pm_code = "P"
															or (not available ptp_det and available pt_mstr and pt_mstr.pt_pm_code = "P"))
               tt_xxcstallhist.xxcstall_curcyccnt         = tt_xxcstallhist.xxcstall_curcyccnt + xxcsttrvar_mstr.xxcsttrvar_amount  
			                                                when (xxcsttrvar_mstr.xxcsttrvar_var_type = "INVLOSS"
 															or xxcsttrvar_mstr.xxcsttrvar_var_type = "INVPROFIT") 
															and (available ptp_det and ptp_det.ptp_pm_code = "P"
															or (not available ptp_det and available pt_mstr and pt_mstr.pt_pm_code = "P"))
               tt_xxcstallhist.xxcstall_curcyccnt_mtl     = tt_xxcstallhist.xxcstall_curcyccnt_mtl + xxcsttrvar_mstr.xxcsttrvar_mtl_std 
			                                                when (xxcsttrvar_mstr.xxcsttrvar_var_type = "INVLOSS" 
															or xxcsttrvar_mstr.xxcsttrvar_var_type = "INVPROFIT")
															and (available ptp_det and ptp_det.ptp_pm_code = "P"
															or (not available ptp_det and available pt_mstr and pt_mstr.pt_pm_code = "P"))
               tt_xxcstallhist.xxcstall_curcyccnt_lbr     = tt_xxcstallhist.xxcstall_curcyccnt_lbr + xxcsttrvar_mstr.xxcsttrvar_lbr_std 
			                                                when (xxcsttrvar_mstr.xxcsttrvar_var_type = "INVLOSS" 
															or xxcsttrvar_mstr.xxcsttrvar_var_type = "INVPROFIT") 
															and (available ptp_det and ptp_det.ptp_pm_code = "P"
															or (not available ptp_det and available pt_mstr and pt_mstr.pt_pm_code = "P"))
               tt_xxcstallhist.xxcstall_curcyccnt_bdn     = tt_xxcstallhist.xxcstall_curcyccnt_bdn + xxcsttrvar_mstr.xxcsttrvar_bdn_std 
			                                                when (xxcsttrvar_mstr.xxcsttrvar_var_type = "INVLOSS" 
															or xxcsttrvar_mstr.xxcsttrvar_var_type = "INVPROFIT") 
															and (available ptp_det and ptp_det.ptp_pm_code = "P"
															or (not available ptp_det and available pt_mstr and pt_mstr.pt_pm_code = "P"))
               tt_xxcstallhist.xxcstall_curcyccnt_ovh     = tt_xxcstallhist.xxcstall_curcyccnt_ovh + xxcsttrvar_mstr.xxcsttrvar_ovh_std 
			                                                when (xxcsttrvar_mstr.xxcsttrvar_var_type = "INVLOSS" 
															or xxcsttrvar_mstr.xxcsttrvar_var_type = "INVPROFIT") 
															and (available ptp_det and ptp_det.ptp_pm_code = "P"
															or (not available ptp_det and available pt_mstr and pt_mstr.pt_pm_code = "P"))
               tt_xxcstallhist.xxcstall_curcyccnt_sub     = tt_xxcstallhist.xxcstall_curcyccnt_sub + xxcsttrvar_mstr.xxcsttrvar_sub_std 
			                                                when (xxcsttrvar_mstr.xxcsttrvar_var_type = "INVLOSS" 
															or xxcsttrvar_mstr.xxcsttrvar_var_type = "INVPROFIT") 
															and (available ptp_det and ptp_det.ptp_pm_code = "P"
															or (not available ptp_det and available pt_mstr and pt_mstr.pt_pm_code = "P"))
               tt_xxcstallhist.xxcstall_curcommon         = xxcsttrvar_mstr.xxcsttrvar_amount  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "COMMON"
															and (available ptp_det and ptp_det.ptp_pm_code = "P"
															or (not available ptp_det and available pt_mstr and pt_mstr.pt_pm_code = "P"))
               tt_xxcstallhist.xxcstall_curothers         = xxcsttrvar_mstr.xxcsttrvar_amount  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "OTHER"
															and (available ptp_det and ptp_det.ptp_pm_code = "P"
															or (not available ptp_det and available pt_mstr and pt_mstr.pt_pm_code = "P")) 
               tt_xxcstallhist.xxcstall_scurmusage        = xxcsttrvar_mstr.xxcsttrvar_amount  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "MATLUSAGE"
               tt_xxcstallhist.xxcstall_scurmrate         = xxcsttrvar_mstr.xxcsttrvar_amount  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "MATLRATE"
               tt_xxcstallhist.xxcstall_scurmmthd         = tt_xxcstallhist.xxcstall_scurmmthd + xxcsttrvar_mstr.xxcsttrvar_amount  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "MTHDCHG_MTL"
															or   xxcsttrvar_mstr.xxcsttrvar_var_type = "MTHDCHG_LBR"
															or   xxcsttrvar_mstr.xxcsttrvar_var_type = "MTHDCHG_BDN"
                                                            or   xxcsttrvar_mstr.xxcsttrvar_var_type = "MTHDCHG_CLS" /* forgot close mthd var */
               tt_xxcstallhist.xxcstall_scurmmthd_mtl     = xxcsttrvar_mstr.xxcsttrvar_amount + xxcsttrvar_mstr.xxcsttrvar_amount  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "MTHDCHG_MTL"
                                                            or   xxcsttrvar_mstr.xxcsttrvar_var_type = "MTHDCHG_CLS"
               tt_xxcstallhist.xxcstall_scurmmthd_lbr     = xxcsttrvar_mstr.xxcsttrvar_amount  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "MTHDCHG_LBR" 
               tt_xxcstallhist.xxcstall_scurmmthd_bdn     = xxcsttrvar_mstr.xxcsttrvar_amount  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "MTHDCHG_BDN"
               tt_xxcstallhist.xxcstall_scurmmthd_ovh     = 0
               tt_xxcstallhist.xxcstall_scurmmthd_sub     = 0
               tt_xxcstallhist.xxcstall_scursubusage      = xxcsttrvar_mstr.xxcsttrvar_amount  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "SUBUSAGE" 
			   /**
               tt_xxcstallhist.xxcstall_scursubusage_mtl  = xxcsttrvar_mstr.xxcsttrvar_mtl_std  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "SUBUSAGE"  
               tt_xxcstallhist.xxcstall_scursubusage_lbr  = xxcsttrvar_mstr.xxcsttrvar_lbr_std
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "SUBUSAGE"   
               tt_xxcstallhist.xxcstall_scursubusage_bdn  = xxcsttrvar_mstr.xxcsttrvar_bdn_std  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "SUBUSAGE"  
               tt_xxcstallhist.xxcstall_scursubusage_ovh  = xxcsttrvar_mstr.xxcsttrvar_ovh_std  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "SUBUSAGE"  
               tt_xxcstallhist.xxcstall_scursubusage_sub  = xxcsttrvar_mstr.xxcsttrvar_amount  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "SUBUSAGE" 
               **/															
               tt_xxcstallhist.xxcstall_scursubrate       = xxcsttrvar_mstr.xxcsttrvar_amount  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "SUBRATE"
               tt_xxcstallhist.xxcstall_scurlbrusage      = xxcsttrvar_mstr.xxcsttrvar_amount  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "LBRUSAGE"
               tt_xxcstallhist.xxcstall_scurlbrrate       = xxcsttrvar_mstr.xxcsttrvar_amount  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "LBRRATE"
               tt_xxcstallhist.xxcstall_scurbdnusage      = xxcsttrvar_mstr.xxcsttrvar_amount  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "BDNUSAGE"
               tt_xxcstallhist.xxcstall_scurbdnrate       = xxcsttrvar_mstr.xxcsttrvar_amount  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "BDNRATE"
               tt_xxcstallhist.xxcstall_scurcstadj        = xxcsttrvar_mstr.xxcsttrvar_amount  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "CSTADJ"
															and ((available ptp_det and (ptp_det.ptp_pm_code = "L" or ptp_det.ptp_pm_code = "")) 
															or (not available ptp_det and (available pt_mstr and pt_mstr.pt_pm_code = "L" or available pt_mstr and pt_mstr.pt_pm_code = "")))
               tt_xxcstallhist.xxcstall_scurcstadj_mtl    = xxcsttrvar_mstr.xxcsttrvar_mtl_std  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "CSTADJ"
															and ((available ptp_det and (ptp_det.ptp_pm_code = "L" or ptp_det.ptp_pm_code = "")) 
															or (not available ptp_det and (available pt_mstr and pt_mstr.pt_pm_code = "L" or available pt_mstr and pt_mstr.pt_pm_code = ""))) 
               tt_xxcstallhist.xxcstall_scurcstadj_lbr    = xxcsttrvar_mstr.xxcsttrvar_lbr_std  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "CSTADJ"
															and ((available ptp_det and (ptp_det.ptp_pm_code = "L" or ptp_det.ptp_pm_code = "")) 
															or (not available ptp_det and (available pt_mstr and pt_mstr.pt_pm_code = "L" or available pt_mstr and pt_mstr.pt_pm_code = "")))
               tt_xxcstallhist.xxcstall_scurcstadj_bdn    = xxcsttrvar_mstr.xxcsttrvar_bdn_std  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "CSTADJ"
															and ((available ptp_det and (ptp_det.ptp_pm_code = "L" or ptp_det.ptp_pm_code = "")) 
															or (not available ptp_det and (available pt_mstr and pt_mstr.pt_pm_code = "L" or available pt_mstr and pt_mstr.pt_pm_code = "")))
               tt_xxcstallhist.xxcstall_scurcstadj_ovh    = xxcsttrvar_mstr.xxcsttrvar_ovh_std  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "CSTADJ"
															and ((available ptp_det and (ptp_det.ptp_pm_code = "L" or ptp_det.ptp_pm_code = "")) 
															or (not available ptp_det and (available pt_mstr and pt_mstr.pt_pm_code = "L" or available pt_mstr and pt_mstr.pt_pm_code = "")))
               tt_xxcstallhist.xxcstall_scurcstadj_sub    = xxcsttrvar_mstr.xxcsttrvar_sub_std  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "CSTADJ"
															and ((available ptp_det and (ptp_det.ptp_pm_code = "L" or ptp_det.ptp_pm_code = "")) 
															or (not available ptp_det and (available pt_mstr and pt_mstr.pt_pm_code = "L" or available pt_mstr and pt_mstr.pt_pm_code = "")))
               tt_xxcstallhist.xxcstall_scurcyccnt        = xxcsttrvar_mstr.xxcsttrvar_amount 
  			                                                when (xxcsttrvar_mstr.xxcsttrvar_var_type = "INVLOSS"
															or   xxcsttrvar_mstr.xxcsttrvar_var_type = "INVPROFIT")
															and ((available ptp_det and (ptp_det.ptp_pm_code = "L" or ptp_det.ptp_pm_code = "")) 
															or (not available ptp_det and (available pt_mstr and pt_mstr.pt_pm_code = "L" or available pt_mstr and pt_mstr.pt_pm_code = "")))
               tt_xxcstallhist.xxcstall_scurcyccnt_mtl    = tt_xxcstallhist.xxcstall_scurcyccnt_mtl + 
			                                                xxcsttrvar_mstr.xxcsttrvar_mtl_std  
			                                                when (xxcsttrvar_mstr.xxcsttrvar_var_type = "INVLOSS"
															or  xxcsttrvar_mstr.xxcsttrvar_var_type = "INVPROFIT")
															and ((available ptp_det and (ptp_det.ptp_pm_code = "L" or ptp_det.ptp_pm_code = "")) 
															or (not available ptp_det and (available pt_mstr and pt_mstr.pt_pm_code = "L" or available pt_mstr and pt_mstr.pt_pm_code = "")))
               tt_xxcstallhist.xxcstall_scurcyccnt_lbr    = tt_xxcstallhist.xxcstall_scurcyccnt_lbr + 
			                                                xxcsttrvar_mstr.xxcsttrvar_lbr_std  
			                                                when (xxcsttrvar_mstr.xxcsttrvar_var_type = "INVLOSS"
															or  xxcsttrvar_mstr.xxcsttrvar_var_type = "INVPROFIT")
			                                                and ((available ptp_det and (ptp_det.ptp_pm_code = "L" or ptp_det.ptp_pm_code = "")) 
															or (not available ptp_det and (available pt_mstr and pt_mstr.pt_pm_code = "L" or available pt_mstr and pt_mstr.pt_pm_code = "")))
               tt_xxcstallhist.xxcstall_scurcyccnt_bdn    = tt_xxcstallhist.xxcstall_scurcyccnt_bdn + 
			                                                xxcsttrvar_mstr.xxcsttrvar_bdn_std  
			                                                when (xxcsttrvar_mstr.xxcsttrvar_var_type = "INVLOSS"
															or  xxcsttrvar_mstr.xxcsttrvar_var_type = "INVPROFIT")
			                                                and ((available ptp_det and (ptp_det.ptp_pm_code = "L" or ptp_det.ptp_pm_code = "")) 
															or (not available ptp_det and (available pt_mstr and pt_mstr.pt_pm_code = "L" or available pt_mstr and pt_mstr.pt_pm_code = "")))
               tt_xxcstallhist.xxcstall_scurcyccnt_ovh    = tt_xxcstallhist.xxcstall_scurcyccnt_ovh + 
			                                                xxcsttrvar_mstr.xxcsttrvar_ovh_std  
			                                                when (xxcsttrvar_mstr.xxcsttrvar_var_type = "INVLOSS"
															or  xxcsttrvar_mstr.xxcsttrvar_var_type = "INVPROFIT")
			                                                and ((available ptp_det and (ptp_det.ptp_pm_code = "L" or ptp_det.ptp_pm_code = "")) 
															or (not available ptp_det and (available pt_mstr and pt_mstr.pt_pm_code = "L" or available pt_mstr and pt_mstr.pt_pm_code = "")))
               tt_xxcstallhist.xxcstall_scurcyccnt_sub    = tt_xxcstallhist.xxcstall_scurcyccnt_sub +
			                                                xxcsttrvar_mstr.xxcsttrvar_sub_std 
			                                                when (xxcsttrvar_mstr.xxcsttrvar_var_type = "INVLOSS"
															or  xxcsttrvar_mstr.xxcsttrvar_var_type = "INVPROFIT")
			                                                and ((available ptp_det and (ptp_det.ptp_pm_code = "L" or ptp_det.ptp_pm_code = "")) 
															or (not available ptp_det and (available pt_mstr and pt_mstr.pt_pm_code = "L" or available pt_mstr and pt_mstr.pt_pm_code = "")))
               tt_xxcstallhist.xxcstall_scurcommon        = xxcsttrvar_mstr.xxcsttrvar_amount  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = "COMMON"
															and ((available ptp_det and (ptp_det.ptp_pm_code = "L" or ptp_det.ptp_pm_code = "")) 
															or (not available ptp_det and (available pt_mstr and pt_mstr.pt_pm_code = "L" or available pt_mstr and pt_mstr.pt_pm_code = "")))
               tt_xxcstallhist.xxcstall_scurothers        = xxcsttrvar_mstr.xxcsttrvar_amount 
                                                			when xxcsttrvar_mstr.xxcsttrvar_var_type = "OTHER"
															and ((available ptp_det and available pt_mstr and (ptp_det.ptp_pm_code = "L" or ptp_det.ptp_pm_code = "")) 
															or (not available ptp_det and (available pt_mstr and pt_mstr.pt_pm_code = "L" or available pt_mstr and pt_mstr.pt_pm_code = "")))
			   /**											
               tt_xxcstallhist.xxcstall_scurraw           = xxcsttrvar_mstr.xxcsttrvar_amount  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = 
               tt_xxcstallhist.xxcstall_scursf            = xxcsttrvar_mstr.xxcsttrvar_amount  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type = 
               tt_xxcstallhist.xxcstall_scursf_mtl        = xxcsttrvar_mstr.xxcsttrvar_amount  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type =  
               tt_xxcstallhist.xxcstall_scursf_lbr        = xxcsttrvar_mstr.xxcsttrvar_amount  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type =  
               tt_xxcstallhist.xxcstall_scursf_bdn        = xxcsttrvar_mstr.xxcsttrvar_amount  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type =  
               tt_xxcstallhist.xxcstall_scursf_ovh        = xxcsttrvar_mstr.xxcsttrvar_amount  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type =  
               tt_xxcstallhist.xxcstall_scursf_sub        = xxcsttrvar_mstr.xxcsttrvar_amount  
			                                                when xxcsttrvar_mstr.xxcsttrvar_var_type =  
				***/
		.			 
		   /*
          	if xxcsttrvar_mstr.xxcsttrvar_part = "37150-F9100" then	
			message tt_xxcstallhist.xxcstall_part tt_xxcstallhist.xxcstall_curppv view-as alert-box.
			*/
			
			/* create tt_xxcstallhist when not available tt_xxcstallhist */
	
	
	
	 end. /* for each xxcsttrinvd_det to copy current variance to tt_xxcstallhist */
	
	/** 1.3 copy current labor burden ovherhead variance to tt_xxcstallhist **/
    for each xxcstctrvar_mstr where xxcstctrvar_mstr.xxcstctrvar_domain = global_domain
				                   and xxcstctrvar_mstr.xxcstctrvar_entity = entity
                                   and xxcstctrvar_mstr.xxcstctrvar_year   = li_year
								   and xxcstctrvar_mstr.xxcstctrvar_period =  li_period
								   and xxcstctrvar_mstr.xxcstctrvar_site = site
								   no-lock.
		   
		   find pt_mstr where pt_part = xxcstctrvar_mstr.xxcstctrvar_part
                           and pt_domain = global_domain 
						   no-lock no-error.
            if debug-prog = yes then
            status default xxcstctrvar_mstr.xxcstctrvar_part + " LaborBurden" 	.
			find tt_xxcstallhist where tt_xxcstallhist.xxcstall_domain  = global_domain
                                   and tt_xxcstallhist.xxcstall_entity   = entity   /* forgot index field entity */
			                       and tt_xxcstallhist.xxcstall_year    = li_year  
	                               and tt_xxcstallhist.xxcstall_period  = li_period
	                               and tt_xxcstallhist.xxcstall_part    = xxcstctrvar_mstr.xxcstctrvar_part
	                               and tt_xxcstallhist.xxcstall_site    = site
								   no-error.
								   
		    if not available tt_xxcstallhist then do:
		    create tt_xxcstallhist.
		    assign 
		       tt_xxcstallhist.xxcstall_domain            = global_domain
		       tt_xxcstallhist.xxcstall_year              = li_year  
               tt_xxcstallhist.xxcstall_period            = li_period
               tt_xxcstallhist.xxcstall_part              = xxcstctrvar_mstr.xxcstctrvar_part
               tt_xxcstallhist.xxcstall_site              = site
               tt_xxcstallhist.xxcstall_prodline          = pt_mstr.pt_prod_line when available pt_mstr
               tt_xxcstallhist.xxcstall_date              = today
               tt_xxcstallhist.xxcstall_time              = time
               tt_xxcstallhist.xxcstall_userid            = global_userid
               tt_xxcstallhist.xxcstall_entity            = entity.						
			end.
            assign
               tt_xxcstallhist.xxcstall_scurlbrrate	= tt_xxcstallhist.xxcstall_scurlbrrate + xxcstctrvar_mstr.xxcstctrvar_lbr_varamt
               tt_xxcstallhist.xxcstall_scurbdnrate = tt_xxcstallhist.xxcstall_scurbdnrate + xxcstctrvar_mstr.xxcstctrvar_bdn_varamt
			   /* need to add new field */
			   tt_xxcstallhist.xxcstall_scurothers = tt_xxcstallhist.xxcstall_scurothers + xxcstctrvar_mstr.xxcstctrvar_ovh_varamt
			  
               .			   							   
	    end. /* end of copy burden labor variance to tt_xxcstallhist */
	 
	 
	 
	 /***** variance allocation mainloop ****/
	 /** 2.1 GET Begining inventory quantity **/
	 
	 for each tt_xxcstallhist where tt_xxcstallhist.xxcstall_domain = global_domain
	                            and tt_xxcstallhist.xxcstall_site   = site
								and tt_xxcstallhist.xxcstall_entity = entity
								and tt_xxcstallhist.xxcstall_year   = li_year
								and tt_xxcstallhist.xxcstall_period = li_period
								.
	  totBeginvqty = 0.
	  totRctinvqty = 0.
	  if debug-prog = yes then
	  status default tt_xxcstallhist.xxcstall_part + " Calculate Rate".
	  
	  Run GetBegInvQty(input tt_xxcstallhist.xxcstall_part,
	                   input tt_xxcstallhist.xxcstall_entity, 
					   input tt_xxcstallhist.xxcstall_site, 
					   input tt_xxcstallhist.xxcstall_year, 
					   input tt_xxcstallhist.xxcstall_period, 
					   output totBeginvqty).
	 
	  Run GetTotalRctQty(input tt_xxcstallhist.xxcstall_part,
	                     input tt_xxcstallhist.xxcstall_entity, 
					     input tt_xxcstallhist.xxcstall_site, 
					     input tt_xxcstallhist.xxcstall_year, 
					     input tt_xxcstallhist.xxcstall_period, 
						 output totRctinvqty). 
						
	 /* 2.2 calculate variance allocation rate */
	 /* Allocation rate = (begin variance + current variance) / (begin inventory + current receipt inventory) */
	 assign
	 tt_xxcstallhist.xxcstall_rateppv          = if (tt_xxcstallhist.xxcstall_begppv + tt_xxcstallhist.xxcstall_curppv) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0
													else
													(tt_xxcstallhist.xxcstall_begppv + tt_xxcstallhist.xxcstall_curppv) /
	                                                (totBeginvqty + totRctinvqty ) 
	 tt_xxcstallhist.xxcstall_ratefreight      = if (tt_xxcstallhist.xxcstall_begfreight + tt_xxcstallhist.xxcstall_curfreight) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_begfreight + tt_xxcstallhist.xxcstall_curfreight) /
													(totBeginvqty + totRctinvqty )
													
     tt_xxcstallhist.xxcstall_rateapusage      = if (tt_xxcstallhist.xxcstall_begapusage + tt_xxcstallhist.xxcstall_curapusage) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_begapusage + tt_xxcstallhist.xxcstall_curapusage) /
													(totBeginvqty + totRctinvqty ) 
     tt_xxcstallhist.xxcstall_rateaprate       = if (tt_xxcstallhist.xxcstall_begaprate + tt_xxcstallhist.xxcstall_curaprate) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_begaprate + tt_xxcstallhist.xxcstall_curaprate) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_ratecstadj       = if (tt_xxcstallhist.xxcstall_begcstadj + tt_xxcstallhist.xxcstall_curcstadj) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_begcstadj + tt_xxcstallhist.xxcstall_curcstadj) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_ratecstadj_mtl   = if (tt_xxcstallhist.xxcstall_begcstadj_mtl + tt_xxcstallhist.xxcstall_curcstadj_mtl) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_begcstadj_mtl + tt_xxcstallhist.xxcstall_curcstadj_mtl) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_ratecstadj_lbr   = if (tt_xxcstallhist.xxcstall_begcstadj_lbr + tt_xxcstallhist.xxcstall_curcstadj_lbr) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_begcstadj_lbr + tt_xxcstallhist.xxcstall_curcstadj_lbr) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_ratecstadj_bdn   = if (tt_xxcstallhist.xxcstall_begcstadj_bdn + tt_xxcstallhist.xxcstall_curcstadj_bdn) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_begcstadj_bdn + tt_xxcstallhist.xxcstall_curcstadj_bdn) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_ratecstadj_ovh   = if (tt_xxcstallhist.xxcstall_begcstadj_ovh + tt_xxcstallhist.xxcstall_curcstadj_ovh) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_begcstadj_ovh + tt_xxcstallhist.xxcstall_curcstadj_ovh) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_ratecstadj_sub   = if (tt_xxcstallhist.xxcstall_begcstadj_sub + tt_xxcstallhist.xxcstall_curcstadj_sub) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_begcstadj_sub + tt_xxcstallhist.xxcstall_curcstadj_sub) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_ratecyccnt       = if (tt_xxcstallhist.xxcstall_begcyccnt + tt_xxcstallhist.xxcstall_curcyccnt) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_begcyccnt + tt_xxcstallhist.xxcstall_curcyccnt) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_ratecyccnt_mtl   = if (tt_xxcstallhist.xxcstall_begcyccnt_mtl + tt_xxcstallhist.xxcstall_curcyccnt_mtl) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_begcyccnt_mtl + tt_xxcstallhist.xxcstall_curcyccnt_mtl) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_ratecyccnt_lbr   = if (tt_xxcstallhist.xxcstall_begcyccnt_lbr + tt_xxcstallhist.xxcstall_curcyccnt_lbr) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_begcyccnt_lbr + tt_xxcstallhist.xxcstall_curcyccnt_lbr) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_ratecyccnt_bdn   = if (tt_xxcstallhist.xxcstall_begcyccnt_bdn + tt_xxcstallhist.xxcstall_curcyccnt_bdn) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_begcyccnt_bdn + tt_xxcstallhist.xxcstall_curcyccnt_bdn) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_ratecyccnt_ovh   = if (tt_xxcstallhist.xxcstall_begcyccnt_ovh + tt_xxcstallhist.xxcstall_curcyccnt_ovh) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_begcyccnt_ovh + tt_xxcstallhist.xxcstall_curcyccnt_ovh) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_ratecyccnt_sub   = if (tt_xxcstallhist.xxcstall_begcyccnt_sub + tt_xxcstallhist.xxcstall_curcyccnt_sub) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_begcyccnt_sub + tt_xxcstallhist.xxcstall_curcyccnt_sub) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_ratecommon       = if (tt_xxcstallhist.xxcstall_begcommon + tt_xxcstallhist.xxcstall_curcommon) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_begcommon + tt_xxcstallhist.xxcstall_curcommon) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_rateothers       = if (tt_xxcstallhist.xxcstall_begothers + tt_xxcstallhist.xxcstall_curothers) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_begothers + tt_xxcstallhist.xxcstall_curothers) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_sratemusage      = if (tt_xxcstallhist.xxcstall_sbegmusage + tt_xxcstallhist.xxcstall_scurmusage) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbegmusage + tt_xxcstallhist.xxcstall_scurmusage) /
													(totBeginvqty + totRctinvqty )
	 /**** material usage variance breakdown into five cost element												
     tt_xxcstallhist.xxcstall_sratemusage_mtl  = if (tt_xxcstallhist.xxcstall_sbegmusage_mtl + tt_xxcstallhist.xxcstall_curapusage) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbegmusage_mtl + tt_xxcstallhist.xxcstall_curapusage) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_sratemusage_lbr  = if (tt_xxcstallhist.xxcstall_sbegmusage_mtl + tt_xxcstallhist.xxcstall_curapusage) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbegmusage_mtl + tt_xxcstallhist.xxcstall_curapusage) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_sratemusage_bdn  = if (tt_xxcstallhist.xxcstall_sbegmusage_mtl + tt_xxcstallhist.xxcstall_curapusage) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbegmusage_mtl + tt_xxcstallhist.xxcstall_curapusage) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_sratemusage_ovh  = if (tt_xxcstallhist.xxcstall_sbegmusage_mtl + tt_xxcstallhist.xxcstall_curapusage) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbegmusage_mtl + tt_xxcstallhist.xxcstall_curapusage) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_sratemusage_sub  = if (tt_xxcstallhist.xxcstall_sbegmusage_mtl + tt_xxcstallhist.xxcstall_curapusage) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbegmusage_mtl + tt_xxcstallhist.xxcstall_curapusage) /
													(totBeginvqty + totRctinvqty )
	*****/											
    
	 tt_xxcstallhist.xxcstall_sratemrate       = if (tt_xxcstallhist.xxcstall_sbegmrate + tt_xxcstallhist.xxcstall_scurmrate) /
													(totBeginvqty + totRctinvqty ) = ? then 0 else 
													(tt_xxcstallhist.xxcstall_sbegmrate + tt_xxcstallhist.xxcstall_scurmrate) /
													(totBeginvqty + totRctinvqty )
													
     tt_xxcstallhist.xxcstall_sratemmthd       = if (tt_xxcstallhist.xxcstall_sbegmmthd + tt_xxcstallhist.xxcstall_scurmmthd) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbegmmthd + tt_xxcstallhist.xxcstall_scurmmthd) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_sratemmthd_mtl   = if (tt_xxcstallhist.xxcstall_sbegmmthd_mtl + tt_xxcstallhist.xxcstall_scurmmthd_mtl) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbegmmthd_mtl + tt_xxcstallhist.xxcstall_scurmmthd_mtl) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_sratemmthd_lbr   = if (tt_xxcstallhist.xxcstall_sbegmmthd_lbr + tt_xxcstallhist.xxcstall_scurmmthd_lbr) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbegmmthd_lbr + tt_xxcstallhist.xxcstall_scurmmthd_lbr) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_sratemmthd_bdn   = if (tt_xxcstallhist.xxcstall_sbegmmthd_bdn + tt_xxcstallhist.xxcstall_scurmmthd_bdn) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbegmmthd_bdn + tt_xxcstallhist.xxcstall_scurmmthd_bdn) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_sratemmthd_ovh   = if (tt_xxcstallhist.xxcstall_sbegmmthd_ovh + tt_xxcstallhist.xxcstall_scurmmthd_ovh) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbegmmthd_ovh + tt_xxcstallhist.xxcstall_scurmmthd_ovh) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_sratemmthd_sub   = if (tt_xxcstallhist.xxcstall_sbegmmthd_sub + tt_xxcstallhist.xxcstall_scurmmthd_sub) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbegmmthd_sub + tt_xxcstallhist.xxcstall_scurmmthd_sub) /
													(totBeginvqty + totRctinvqty )											
     tt_xxcstallhist.xxcstall_sratesubusage    = if (tt_xxcstallhist.xxcstall_sbegsubusage + tt_xxcstallhist.xxcstall_scursubusage) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbegsubusage + tt_xxcstallhist.xxcstall_scursubusage) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_sratesubrate     = if (tt_xxcstallhist.xxcstall_sbegsubrate + tt_xxcstallhist.xxcstall_scursubrate) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbegsubrate + tt_xxcstallhist.xxcstall_scursubrate) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_sratelbrusage    = if (tt_xxcstallhist.xxcstall_sbeglbrusage + tt_xxcstallhist.xxcstall_scurlbrusage) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbeglbrusage + tt_xxcstallhist.xxcstall_scurlbrusage) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_sratelbrrate     = if (tt_xxcstallhist.xxcstall_sbeglbrrate + tt_xxcstallhist.xxcstall_scurlbrrate) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbeglbrrate + tt_xxcstallhist.xxcstall_scurlbrrate) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_sratebdnusage    = if (tt_xxcstallhist.xxcstall_sbegbdnusage + tt_xxcstallhist.xxcstall_scurbdnusage) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbegbdnusage + tt_xxcstallhist.xxcstall_scurbdnusage) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_sratebdnrate     = if (tt_xxcstallhist.xxcstall_sbegbdnrate + tt_xxcstallhist.xxcstall_scurbdnrate) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbegbdnrate + tt_xxcstallhist.xxcstall_scurbdnrate) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_sratecstadj      = if (tt_xxcstallhist.xxcstall_sbegcstadj + tt_xxcstallhist.xxcstall_scurcstadj) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbegcstadj + tt_xxcstallhist.xxcstall_scurcstadj) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_sratecstadj_mtl  = if (tt_xxcstallhist.xxcstall_sbegcstadj_mtl + tt_xxcstallhist.xxcstall_scurcstadj_mtl) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbegcstadj_mtl + tt_xxcstallhist.xxcstall_scurcstadj_mtl) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_sratecstadj_lbr  = if (tt_xxcstallhist.xxcstall_sbegcstadj_lbr + tt_xxcstallhist.xxcstall_scurcstadj_lbr) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbegcstadj_lbr + tt_xxcstallhist.xxcstall_scurcstadj_lbr) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_sratecstadj_bdn  = if (tt_xxcstallhist.xxcstall_sbegcstadj_bdn + tt_xxcstallhist.xxcstall_scurcstadj_bdn) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbegcstadj_bdn + tt_xxcstallhist.xxcstall_scurcstadj_bdn) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_sratecstadj_ovh  = if (tt_xxcstallhist.xxcstall_sbegcstadj_ovh + tt_xxcstallhist.xxcstall_scurcstadj_ovh) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbegcstadj_ovh + tt_xxcstallhist.xxcstall_scurcstadj_ovh) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_sratecstadj_sub  = if (tt_xxcstallhist.xxcstall_sbegcstadj_sub + tt_xxcstallhist.xxcstall_scurcstadj_sub) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbegcstadj_sub + tt_xxcstallhist.xxcstall_scurcstadj_sub) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_sratecyccnt      = if (tt_xxcstallhist.xxcstall_sbegcyccnt + tt_xxcstallhist.xxcstall_scurcyccnt) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbegcyccnt + tt_xxcstallhist.xxcstall_scurcyccnt) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_sratecyccnt_mtl  = if (tt_xxcstallhist.xxcstall_sbegcyccnt_mtl + tt_xxcstallhist.xxcstall_scurcyccnt_mtl) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbegcyccnt_mtl + tt_xxcstallhist.xxcstall_scurcyccnt_mtl) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_sratecyccnt_lbr  = if (tt_xxcstallhist.xxcstall_sbegcyccnt_lbr + tt_xxcstallhist.xxcstall_scurcyccnt_lbr) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbegcyccnt_lbr + tt_xxcstallhist.xxcstall_scurcyccnt_lbr) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_sratecyccnt_bdn  = if (tt_xxcstallhist.xxcstall_sbegcyccnt_bdn + tt_xxcstallhist.xxcstall_scurcyccnt_bdn) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbegcyccnt_bdn + tt_xxcstallhist.xxcstall_scurcyccnt_bdn) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_sratecyccnt_ovh  = if (tt_xxcstallhist.xxcstall_sbegcyccnt_ovh + tt_xxcstallhist.xxcstall_scurcyccnt_ovh) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbegcyccnt_ovh + tt_xxcstallhist.xxcstall_scurcyccnt_ovh) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_sratecyccnt_sub  = if (tt_xxcstallhist.xxcstall_sbegcyccnt_sub + tt_xxcstallhist.xxcstall_scurcyccnt_sub) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbegcyccnt_sub + tt_xxcstallhist.xxcstall_scurcyccnt_sub) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_sratecommon      = if (tt_xxcstallhist.xxcstall_sbegcommon + tt_xxcstallhist.xxcstall_scurcommon) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbegcommon + tt_xxcstallhist.xxcstall_scurcommon) /
													(totBeginvqty + totRctinvqty )
     tt_xxcstallhist.xxcstall_srateothers      = if (tt_xxcstallhist.xxcstall_sbegothers + tt_xxcstallhist.xxcstall_scurothers) /
	                                                (totBeginvqty + totRctinvqty ) = ? then 0 else
													(tt_xxcstallhist.xxcstall_sbegothers + tt_xxcstallhist.xxcstall_scurothers) /
													(totBeginvqty + totRctinvqty )
     
	 .
	 
	 /* 2.3 issue variance amount calculate */
	 
	 /*** Purchase variance allocation for iss-unp */
	 totrqty = 0.
	 Run GetTrQty(input tt_xxcstallhist.xxcstall_part,
	                input "ISS-UNP",
					output totrqty).
	 assign
          tt_xxcstallhist.xxcstall_curissunpppv           =  - totrqty * tt_xxcstallhist.xxcstall_rateppv
          tt_xxcstallhist.xxcstall_curissunpfreight       =  - totrqty * tt_xxcstallhist.xxcstall_ratefreight
          tt_xxcstallhist.xxcstall_curissunpapusage       =  - totrqty * tt_xxcstallhist.xxcstall_rateapusage
          tt_xxcstallhist.xxcstall_curissunpaprate        =  - totrqty * tt_xxcstallhist.xxcstall_rateaprate
          tt_xxcstallhist.xxcstall_curissunpcstadj        =  - totrqty * tt_xxcstallhist.xxcstall_ratecstadj
          tt_xxcstallhist.xxcstall_curissunpcstadj_mtl    =  - totrqty * tt_xxcstallhist.xxcstall_ratecstadj_mtl
          tt_xxcstallhist.xxcstall_curissunpcstadj_lbr    =  - totrqty * tt_xxcstallhist.xxcstall_ratecstadj_lbr
          tt_xxcstallhist.xxcstall_curissunpcstadj_bdn    =  - totrqty * tt_xxcstallhist.xxcstall_ratecstadj_bdn
          tt_xxcstallhist.xxcstall_curissunpcstadj_ovh    =  - totrqty * tt_xxcstallhist.xxcstall_ratecstadj_ovh
          tt_xxcstallhist.xxcstall_curissunpcstadj_sub    =  - totrqty * tt_xxcstallhist.xxcstall_ratecstadj_sub
          /*
          tt_xxcstallhist.xxcstall_curissunpcyccnt        =  - totrqty * tt_xxcstallhist.xxcstall_ratecyccnt    
          tt_xxcstallhist.xxcstall_curissunpcyccnt_mtl    =  - totrqty * tt_xxcstallhist.xxcstall_ratecyccnt_mtl
          tt_xxcstallhist.xxcstall_curissunpcyccnt_lbr    =  - totrqty * tt_xxcstallhist.xxcstall_ratecyccnt_lbr
          tt_xxcstallhist.xxcstall_curissunpcyccnt_bdn    =  - totrqty * tt_xxcstallhist.xxcstall_ratecyccnt_bdn
          tt_xxcstallhist.xxcstall_curissunpcyccnt_ovh    =  - totrqty * tt_xxcstallhist.xxcstall_ratecyccnt_ovh
          tt_xxcstallhist.xxcstall_curissunpcyccnt_sub    =  - totrqty * tt_xxcstallhist.xxcstall_ratecyccnt_sub
          */
		  tt_xxcstallhist.xxcstall_curissunpcommon        =  - totrqty * tt_xxcstallhist.xxcstall_ratecommon    
          tt_xxcstallhist.xxcstall_curissunpothers        =  - totrqty * tt_xxcstallhist.xxcstall_rateothers   
     	  
		  .
		 /*** Purchase variance allocation for iss-wo */
	     totrqty= 0.
         Run GetTrQty(input tt_xxcstallhist.xxcstall_part,
	                    input "ISS-WO",
	     				output totrqty).		  
         Run GetSameISSRCTWO(input tt_xxcstallhist.xxcstall_part,
                             input entity,
                             input site,
                             input li_year,
                             input li_period,
                             output totissqty).
				totrqty = totrqty - totissqty.	
		 assign
          tt_xxcstallhist.xxcstall_curisswoppv            =  - totrqty * tt_xxcstallhist.xxcstall_rateppv        
          tt_xxcstallhist.xxcstall_curisswofreight        =  - totrqty * tt_xxcstallhist.xxcstall_ratefreight    
          tt_xxcstallhist.xxcstall_curisswoapusage        =  - totrqty * tt_xxcstallhist.xxcstall_rateapusage    
          tt_xxcstallhist.xxcstall_curisswoaprate         =  - totrqty * tt_xxcstallhist.xxcstall_rateaprate     
          tt_xxcstallhist.xxcstall_curisswocstadj         =  - totrqty * tt_xxcstallhist.xxcstall_ratecstadj     
          tt_xxcstallhist.xxcstall_curisswocstadj_mtl     =  - totrqty * tt_xxcstallhist.xxcstall_ratecstadj_mtl 
          tt_xxcstallhist.xxcstall_curisswocstadj_lbr     =  - totrqty * tt_xxcstallhist.xxcstall_ratecstadj_lbr 
          tt_xxcstallhist.xxcstall_curisswocstadj_bdn     =  - totrqty * tt_xxcstallhist.xxcstall_ratecstadj_bdn 
          tt_xxcstallhist.xxcstall_curisswocstadj_ovh     =  - totrqty * tt_xxcstallhist.xxcstall_ratecstadj_ovh 
          tt_xxcstallhist.xxcstall_curisswocstadj_sub     =  - totrqty * tt_xxcstallhist.xxcstall_ratecstadj_sub 
          /*
          tt_xxcstallhist.xxcstall_curisswocyccnt         =  - totrqty * tt_xxcstallhist.xxcstall_ratecyccnt     
          tt_xxcstallhist.xxcstall_curisswocyccnt_mtl     =  - totrqty * tt_xxcstallhist.xxcstall_ratecyccnt_mtl 
          tt_xxcstallhist.xxcstall_curisswocyccnt_lbr     =  - totrqty * tt_xxcstallhist.xxcstall_ratecyccnt_lbr 
          tt_xxcstallhist.xxcstall_curisswocyccnt_bdn     =  - totrqty * tt_xxcstallhist.xxcstall_ratecyccnt_bdn 
          tt_xxcstallhist.xxcstall_curisswocyccnt_ovh     =  - totrqty * tt_xxcstallhist.xxcstall_ratecyccnt_ovh 
          tt_xxcstallhist.xxcstall_curisswocyccnt_sub     =  - totrqty * tt_xxcstallhist.xxcstall_ratecyccnt_sub 
          */
		  tt_xxcstallhist.xxcstall_curisswocommon         =  - totrqty * tt_xxcstallhist.xxcstall_ratecommon     
          tt_xxcstallhist.xxcstall_curisswoothers         =  - totrqty * tt_xxcstallhist.xxcstall_rateothers   
     	  
		  .
		 /*** Purchase variance allocation for count */
	     totrqty= 0.
         Run GetTrQty(input tt_xxcstallhist.xxcstall_part,
	                    input "INVLOSS",
	     				output totrqty).		  
          assign
		  tt_xxcstallhist.xxcstall_curcyccntppv           =  - totrqty * tt_xxcstallhist.xxcstall_rateppv       
          tt_xxcstallhist.xxcstall_curcyccntfreight       =  - totrqty * tt_xxcstallhist.xxcstall_ratefreight   
          tt_xxcstallhist.xxcstall_curcyccntapusage       =  - totrqty * tt_xxcstallhist.xxcstall_rateapusage   
          tt_xxcstallhist.xxcstall_curcyccntaprate        =  - totrqty * tt_xxcstallhist.xxcstall_rateaprate    
          tt_xxcstallhist.xxcstall_curcyccntcstadj        =  - totrqty * tt_xxcstallhist.xxcstall_ratecstadj    
          tt_xxcstallhist.xxcstall_curcyccntcstadj_mtl    =  - totrqty * tt_xxcstallhist.xxcstall_ratecstadj_mtl
          tt_xxcstallhist.xxcstall_curcyccntcstadj_lbr    =  - totrqty * tt_xxcstallhist.xxcstall_ratecstadj_lbr
          tt_xxcstallhist.xxcstall_curcyccntcstadj_bdn    =  - totrqty * tt_xxcstallhist.xxcstall_ratecstadj_bdn
          tt_xxcstallhist.xxcstall_curcyccntcstadj_ovh    =  - totrqty * tt_xxcstallhist.xxcstall_ratecstadj_ovh
          tt_xxcstallhist.xxcstall_curcyccntcstadj_sub    =  - totrqty * tt_xxcstallhist.xxcstall_ratecstadj_sub
          /*
		  tt_xxcstallhist.xxcstall_curcyccntcyccnt        =  - totrqty * tt_xxcstallhist.xxcstall_ratecyccnt    
          tt_xxcstallhist.xxcstall_curcyccntcyccnt_mtl    =  - totrqty * tt_xxcstallhist.xxcstall_ratecyccnt_mtl
          tt_xxcstallhist.xxcstall_curcyccntcyccnt_lbr    =  - totrqty * tt_xxcstallhist.xxcstall_ratecyccnt_lbr
          tt_xxcstallhist.xxcstall_curcyccntcyccnt_bdn    =  - totrqty * tt_xxcstallhist.xxcstall_ratecyccnt_bdn
          tt_xxcstallhist.xxcstall_curcyccntcyccnt_ovh    =  - totrqty * tt_xxcstallhist.xxcstall_ratecyccnt_ovh
          tt_xxcstallhist.xxcstall_curcyccntcyccnt_sub    =  - totrqty * tt_xxcstallhist.xxcstall_ratecyccnt_sub
          */
		  tt_xxcstallhist.xxcstall_curcyccntcommon        =  - totrqty * tt_xxcstallhist.xxcstall_ratecommon    
          tt_xxcstallhist.xxcstall_curcyccntothers        =  - totrqty * tt_xxcstallhist.xxcstall_rateothers    
          
          .		  
		 /*** Purchase variance allocation for ISS-SO ***/  
		 totrqty= 0.
         Run GetTrQty(input tt_xxcstallhist.xxcstall_part,
	                    input "ISS-SO",
	     				output totrqty).	
          assign
		  tt_xxcstallhist.xxcstall_curisssoppv            =  - totrqty * tt_xxcstallhist.xxcstall_rateppv       
          tt_xxcstallhist.xxcstall_curisssofreight        =  - totrqty * tt_xxcstallhist.xxcstall_ratefreight   
          tt_xxcstallhist.xxcstall_curisssoapusage        =  - totrqty * tt_xxcstallhist.xxcstall_rateapusage   
          tt_xxcstallhist.xxcstall_curisssoaprate         =  - totrqty * tt_xxcstallhist.xxcstall_rateaprate    
          tt_xxcstallhist.xxcstall_curisssocstadj         =  - totrqty * tt_xxcstallhist.xxcstall_ratecstadj    
          tt_xxcstallhist.xxcstall_curisssocstadj_mtl     =  - totrqty * tt_xxcstallhist.xxcstall_ratecstadj_mtl
          tt_xxcstallhist.xxcstall_curisssocstadj_lbr     =  - totrqty * tt_xxcstallhist.xxcstall_ratecstadj_lbr
          tt_xxcstallhist.xxcstall_curisssocstadj_ovh     =  - totrqty * tt_xxcstallhist.xxcstall_ratecstadj_ovh
          tt_xxcstallhist.xxcstall_curisssocstadj_bdn     =  - totrqty * tt_xxcstallhist.xxcstall_ratecstadj_bdn
          tt_xxcstallhist.xxcstall_curisssocstadj_sub     =  - totrqty * tt_xxcstallhist.xxcstall_ratecstadj_sub
          /*
		  tt_xxcstallhist.xxcstall_curisssocyccnt         =  - totrqty * tt_xxcstallhist.xxcstall_ratecyccnt    
          tt_xxcstallhist.xxcstall_curisssocyccnt_mtl     =  - totrqty * tt_xxcstallhist.xxcstall_ratecyccnt_mtl
          tt_xxcstallhist.xxcstall_curisssocyccnt_lbr     =  - totrqty * tt_xxcstallhist.xxcstall_ratecyccnt_lbr
          tt_xxcstallhist.xxcstall_curisssocyccnt_bdn     =  - totrqty * tt_xxcstallhist.xxcstall_ratecyccnt_bdn
          tt_xxcstallhist.xxcstall_curisssocyccnt_ovh     =  - totrqty * tt_xxcstallhist.xxcstall_ratecyccnt_ovh
          tt_xxcstallhist.xxcstall_curisssocyccnt_sub     =  - totrqty * tt_xxcstallhist.xxcstall_ratecyccnt_sub
		  */
		  tt_xxcstallhist.xxcstall_curisssocommon         =  - totrqty * tt_xxcstallhist.xxcstall_ratecommon    
          tt_xxcstallhist.xxcstall_curisssoothers         =  - totrqty * tt_xxcstallhist.xxcstall_rateothers 
		  
		  .
		 /*** Manufacture variance allocation for ISS-UNP ***/  
		   totrqty= 0.
           Run GetTrQty(input tt_xxcstallhist.xxcstall_part,
	                    input "ISS-UNP",
	     				output totrqty).			  
		  assign
          tt_xxcstallhist.xxcstall_scurissunpmusage       =  - totrqty * tt_xxcstallhist.xxcstall_sratemusage   
          /*		                                                                                 
          tt_xxcstallhist.xxcstall_scurissunpmusage_mtl   =  - totrqty * tt_xxcstallhist.xxcstall_sratemusage_mtl
          tt_xxcstallhist.xxcstall_scurissunpmusage_lbr   =  - totrqty * tt_xxcstallhist.xxcstall_sratemusage_lbr
          tt_xxcstallhist.xxcstall_scurissunpmusage_bdn   =  - totrqty * tt_xxcstallhist.xxcstall_sratemusage_bdn
          tt_xxcstallhist.xxcstall_scurissunpmusage_ovh   =  - totrqty * tt_xxcstallhist.xxcstall_sratemusage_ovh
          tt_xxcstallhist.xxcstall_scurissunpmusage_sub   =  - totrqty * tt_xxcstallhist.xxcstall_sratemusage_sub
          */                                                                                         
          tt_xxcstallhist.xxcstall_scurissunpmrate        =  - totrqty * tt_xxcstallhist.xxcstall_sratemrate     
          tt_xxcstallhist.xxcstall_scurissunpmmthd        =  - totrqty * tt_xxcstallhist.xxcstall_sratemmthd     
          tt_xxcstallhist.xxcstall_scurissunpmmthd_mtl    =  - totrqty * tt_xxcstallhist.xxcstall_sratemmthd_mtl 
          tt_xxcstallhist.xxcstall_scurissunpmmthd_lbr    =  - totrqty * tt_xxcstallhist.xxcstall_sratemmthd_lbr 
          tt_xxcstallhist.xxcstall_scurissunpmmthd_bdn    =  - totrqty * tt_xxcstallhist.xxcstall_sratemmthd_bdn 
          tt_xxcstallhist.xxcstall_scurissunpmmthd_ovh    =  - totrqty * tt_xxcstallhist.xxcstall_sratemmthd_ovh 
          tt_xxcstallhist.xxcstall_scurissunpmmthd_sub    =  - totrqty * tt_xxcstallhist.xxcstall_sratemmthd_sub 
          tt_xxcstallhist.xxcstall_scurissunpsubusage     =  - totrqty * tt_xxcstallhist.xxcstall_sratesubusage  
          tt_xxcstallhist.xxcstall_scurissunpsubrate      =  - totrqty * tt_xxcstallhist.xxcstall_sratesubrate   
          tt_xxcstallhist.xxcstall_scurissunplbrusage     =  - totrqty * tt_xxcstallhist.xxcstall_sratelbrusage  
          tt_xxcstallhist.xxcstall_scurissunplbrrate      =  - totrqty * tt_xxcstallhist.xxcstall_sratelbrrate   
          tt_xxcstallhist.xxcstall_scurissunpbdnusage     =  - totrqty * tt_xxcstallhist.xxcstall_sratebdnusage  
          tt_xxcstallhist.xxcstall_scurissunpbdnrate      =  - totrqty * tt_xxcstallhist.xxcstall_sratebdnrate   
          tt_xxcstallhist.xxcstall_scurissunpcstadj       =  - totrqty * tt_xxcstallhist.xxcstall_sratecstadj    
          tt_xxcstallhist.xxcstall_scurissunpcstadj_mtl   =  - totrqty * tt_xxcstallhist.xxcstall_sratecstadj_mtl
          tt_xxcstallhist.xxcstall_scurissunpcstadj_lbr   =  - totrqty * tt_xxcstallhist.xxcstall_sratecstadj_lbr
          tt_xxcstallhist.xxcstall_scurissunpcstadj_ovh   =  - totrqty * tt_xxcstallhist.xxcstall_sratecstadj_ovh
          tt_xxcstallhist.xxcstall_scurissunpcstadj_bdn   =  - totrqty * tt_xxcstallhist.xxcstall_sratecstadj_bdn
          tt_xxcstallhist.xxcstall_scurissunpcstadj_sub   =  - totrqty * tt_xxcstallhist.xxcstall_sratecstadj_sub
          /*                                                                                         
          tt_xxcstallhist.xxcstall_scurissunpcyccnt       =  - totrqty * tt_xxcstallhist.xxcstall_sratecyccnt    
          tt_xxcstallhist.xxcstall_scurissunpcyccnt_mtl   =  - totrqty * tt_xxcstallhist.xxcstall_sratecyccnt_mtl
          tt_xxcstallhist.xxcstall_scurissunpcyccnt_lbr   =  - totrqty * tt_xxcstallhist.xxcstall_sratecyccnt_lbr
          tt_xxcstallhist.xxcstall_scurissunpcyccnt_bdn   =  - totrqty * tt_xxcstallhist.xxcstall_sratecyccnt_bdn
          tt_xxcstallhist.xxcstall_scurissunpcyccnt_ovh   =  - totrqty * tt_xxcstallhist.xxcstall_sratecyccnt_ovh
          tt_xxcstallhist.xxcstall_scurissunpcyccnt_sub   =  - totrqty * tt_xxcstallhist.xxcstall_sratecyccnt_sub
		  */
          tt_xxcstallhist.xxcstall_scurissunpcommon       =  - totrqty * tt_xxcstallhist.xxcstall_sratecommon    
          /*		                                                                                     
          tt_xxcstallhist.xxcstall_scurissunpraw          =  - totrqty * tt_xxcstallhist.xxcstall_srate
          tt_xxcstallhist.xxcstall_scurissunpsf           =  - totrqty * tt_xxcstallhist.xxcstall_srate
          tt_xxcstallhist.xxcstall_scurissunpsf_mtl       =  - totrqty * tt_xxcstallhist.xxcstall_srate
          tt_xxcstallhist.xxcstall_scurissunpsf_lbr       =  - totrqty * tt_xxcstallhist.xxcstall_srate
          tt_xxcstallhist.xxcstall_scurissunpsf_bdn       =  - totrqty * tt_xxcstallhist.xxcstall_srate
          tt_xxcstallhist.xxcstall_scurissunpsf_ovh       =  - totrqty * tt_xxcstallhist.xxcstall_srate
          tt_xxcstallhist.xxcstall_scurissunpsf_sub       =  - totrqty * tt_xxcstallhist.xxcstall_srate
          */
		  tt_xxcstallhist.xxcstall_scurissunpothers       =  - totrqty * tt_xxcstallhist.xxcstall_srateothers   
		  .
		 /*** Manufacture variance allocation for ISS-WO ***/  
		 totrqty= 0.
         Run GetTrQty(input tt_xxcstallhist.xxcstall_part,
	                    input "ISS-WO",
	     				output totrqty).			  
                   Run GetSameISSRCTWO(input tt_xxcstallhist.xxcstall_part,
                             input entity,
                             input site,
                             input li_year,
                             input li_period,
                             output totissqty).
                totrqty = totrqty - totissqty.	
		  assign
		  tt_xxcstallhist.xxcstall_scurisswomusage        =  - totrqty * tt_xxcstallhist.xxcstall_sratemusage  
		  /*                                                                                                     
          tt_xxcstallhist.xxcstall_scurisswomusage_mtl    =  - totrqty * tt_xxcstallhist.xxcstall_sratemusage_mtl
          tt_xxcstallhist.xxcstall_scurisswomusage_lbr    =  - totrqty * tt_xxcstallhist.xxcstall_sratemusage_lbr
          tt_xxcstallhist.xxcstall_scurisswomusage_bdn    =  - totrqty * tt_xxcstallhist.xxcstall_sratemusage_bdn
          tt_xxcstallhist.xxcstall_scurisswomusage_ovh    =  - totrqty * tt_xxcstallhist.xxcstall_sratemusage_ovh
          tt_xxcstallhist.xxcstall_scurisswomusage_sub    =  - totrqty * tt_xxcstallhist.xxcstall_sratemusage_sub
		  */                                                                                                       
          tt_xxcstallhist.xxcstall_scurisswomrate         =  - totrqty * tt_xxcstallhist.xxcstall_sratemrate     
          tt_xxcstallhist.xxcstall_scurisswommthd         =  - totrqty * tt_xxcstallhist.xxcstall_sratemmthd     
          tt_xxcstallhist.xxcstall_scurisswommthd_mtl     =  - totrqty * tt_xxcstallhist.xxcstall_sratemmthd_mtl 
          tt_xxcstallhist.xxcstall_scurisswommthd_lbr     =  - totrqty * tt_xxcstallhist.xxcstall_sratemmthd_lbr 
          tt_xxcstallhist.xxcstall_scurisswommthd_bdn     =  - totrqty * tt_xxcstallhist.xxcstall_sratemmthd_bdn 
          tt_xxcstallhist.xxcstall_scurisswommthd_ovh     =  - totrqty * tt_xxcstallhist.xxcstall_sratemmthd_ovh 
          tt_xxcstallhist.xxcstall_scurisswommthd_sub     =  - totrqty * tt_xxcstallhist.xxcstall_sratemmthd_sub 
          tt_xxcstallhist.xxcstall_scurisswosubusage      =  - totrqty * tt_xxcstallhist.xxcstall_sratesubusage  
          tt_xxcstallhist.xxcstall_scurisswosubrate       =  - totrqty * tt_xxcstallhist.xxcstall_sratesubrate   
          tt_xxcstallhist.xxcstall_scurisswolbrusage      =  - totrqty * tt_xxcstallhist.xxcstall_sratelbrusage  
          tt_xxcstallhist.xxcstall_scurisswolbrrate       =  - totrqty * tt_xxcstallhist.xxcstall_sratelbrrate   
          tt_xxcstallhist.xxcstall_scurisswobdnusage      =  - totrqty * tt_xxcstallhist.xxcstall_sratebdnusage  
          tt_xxcstallhist.xxcstall_scurisswobdnrate       =  - totrqty * tt_xxcstallhist.xxcstall_sratebdnrate   
          tt_xxcstallhist.xxcstall_scurisswocstadj        =  - totrqty * tt_xxcstallhist.xxcstall_sratecstadj    
          tt_xxcstallhist.xxcstall_scurisswocstadj_mtl    =  - totrqty * tt_xxcstallhist.xxcstall_sratecstadj_mtl
          tt_xxcstallhist.xxcstall_scurisswocstadj_lbr    =  - totrqty * tt_xxcstallhist.xxcstall_sratecstadj_lbr
          tt_xxcstallhist.xxcstall_scurisswocstadj_ovh    =  - totrqty * tt_xxcstallhist.xxcstall_sratecstadj_ovh
          tt_xxcstallhist.xxcstall_scurisswocstadj_bdn    =  - totrqty * tt_xxcstallhist.xxcstall_sratecstadj_bdn
          tt_xxcstallhist.xxcstall_scurisswocstadj_sub    =  - totrqty * tt_xxcstallhist.xxcstall_sratecstadj_sub
          /*                                                                                                      
		  tt_xxcstallhist.xxcstall_scurisswocyccnt        =  - totrqty * tt_xxcstallhist.xxcstall_sratecyccnt    
          tt_xxcstallhist.xxcstall_scurisswocyccnt_mtl    =  - totrqty * tt_xxcstallhist.xxcstall_sratecyccnt_mtl
          tt_xxcstallhist.xxcstall_scurisswocyccnt_lbr    =  - totrqty * tt_xxcstallhist.xxcstall_sratecyccnt_lbr
          tt_xxcstallhist.xxcstall_scurisswocyccnt_bdn    =  - totrqty * tt_xxcstallhist.xxcstall_sratecyccnt_bdn
          tt_xxcstallhist.xxcstall_scurisswocyccnt_ovh    =  - totrqty * tt_xxcstallhist.xxcstall_sratecyccnt_ovh
          tt_xxcstallhist.xxcstall_scurisswocyccnt_sub    =  - totrqty * tt_xxcstallhist.xxcstall_sratecyccnt_sub
		  */
		  tt_xxcstallhist.xxcstall_scurisswocommon        =  - totrqty * tt_xxcstallhist.xxcstall_sratecommon    
          /*
          tt_xxcstallhist.xxcstall_scurissworaw           =  - totrqty * tt_xxcstallhist.xxcstall_srateraw       
          tt_xxcstallhist.xxcstall_scurisswosf            =  - totrqty * tt_xxcstallhist.xxcstall_sratesf        
          tt_xxcstallhist.xxcstall_scurisswosf_mtl        =  - totrqty * tt_xxcstallhist.xxcstall_sratesf_mtl    
          tt_xxcstallhist.xxcstall_scurisswosf_lbr        =  - totrqty * tt_xxcstallhist.xxcstall_sratesf_lbr    
          tt_xxcstallhist.xxcstall_scurisswosf_bdn        =  - totrqty * tt_xxcstallhist.xxcstall_sratesf_bdn    
          tt_xxcstallhist.xxcstall_scurisswosf_ovh        =  - totrqty * tt_xxcstallhist.xxcstall_sratesf_ovh    
          tt_xxcstallhist.xxcstall_scurisswosf_sub        =  - totrqty * tt_xxcstallhist.xxcstall_sratesf_sub   
		  */
          tt_xxcstallhist.xxcstall_scurisswoothers        =  - totrqty * tt_xxcstallhist.xxcstall_srateothers 
          .		  
		 /*** Manufacture variance allocation for Count ***/  
		 totrqty= 0.
         Run GetTrQty(input tt_xxcstallhist.xxcstall_part,
	                    input "INVLOSS",
	     				output totrqty).
          assign
		  tt_xxcstallhist.xxcstall_scurcyccntmusage       =  - totrqty * tt_xxcstallhist.xxcstall_sratemusage   
          /*		  
          tt_xxcstallhist.xxcstall_scurcyccntmusage_mtl   =  - totrqty * tt_xxcstallhist.xxcstall_sratemusage_mtl
          tt_xxcstallhist.xxcstall_scurcyccntmusage_lbr   =  - totrqty * tt_xxcstallhist.xxcstall_sratemusage_lbr
          tt_xxcstallhist.xxcstall_scurcyccntmusage_bdn   =  - totrqty * tt_xxcstallhist.xxcstall_sratemusage_bdn
          tt_xxcstallhist.xxcstall_scurcyccntmusage_ovh   =  - totrqty * tt_xxcstallhist.xxcstall_sratemusage_ovh
          tt_xxcstallhist.xxcstall_scurcyccntmusage_sub   =  - totrqty * tt_xxcstallhist.xxcstall_sratemusage_sub
		  */
          tt_xxcstallhist.xxcstall_scurcyccntmrate        =  - totrqty * tt_xxcstallhist.xxcstall_sratemrate     
          tt_xxcstallhist.xxcstall_scurcyccntmmthd        =  - totrqty * tt_xxcstallhist.xxcstall_sratemmthd     
          tt_xxcstallhist.xxcstall_scurcyccntmmthd_mtl    =  - totrqty * tt_xxcstallhist.xxcstall_sratemmthd_mtl 
          tt_xxcstallhist.xxcstall_scurcyccntmmthd_lbr    =  - totrqty * tt_xxcstallhist.xxcstall_sratemmthd_lbr 
          tt_xxcstallhist.xxcstall_scurcyccntmmthd_bdn    =  - totrqty * tt_xxcstallhist.xxcstall_sratemmthd_bdn 
          tt_xxcstallhist.xxcstall_scurcyccntmmthd_ovh    =  - totrqty * tt_xxcstallhist.xxcstall_sratemmthd_ovh 
          tt_xxcstallhist.xxcstall_scurcyccntmmthd_sub    =  - totrqty * tt_xxcstallhist.xxcstall_sratemmthd_sub 
          tt_xxcstallhist.xxcstall_scurcyccntsubusage     =  - totrqty * tt_xxcstallhist.xxcstall_sratesubusage  
          tt_xxcstallhist.xxcstall_scurcyccntsubrate      =  - totrqty * tt_xxcstallhist.xxcstall_sratesubrate   
          tt_xxcstallhist.xxcstall_scurcyccntlbrusage     =  - totrqty * tt_xxcstallhist.xxcstall_sratelbrusage  
          tt_xxcstallhist.xxcstall_scurcyccntlbrrate      =  - totrqty * tt_xxcstallhist.xxcstall_sratelbrrate   
          tt_xxcstallhist.xxcstall_scurcyccntbdnusage     =  - totrqty * tt_xxcstallhist.xxcstall_sratebdnusage  
          tt_xxcstallhist.xxcstall_scurcyccntbdnrate      =  - totrqty * tt_xxcstallhist.xxcstall_sratebdnrate   
          tt_xxcstallhist.xxcstall_scurcyccntcstadj       =  - totrqty * tt_xxcstallhist.xxcstall_sratecstadj    
          tt_xxcstallhist.xxcstall_scurcyccntcstadj_mtl   =  - totrqty * tt_xxcstallhist.xxcstall_sratecstadj_mtl
          tt_xxcstallhist.xxcstall_scurcyccntcstadj_lbr   =  - totrqty * tt_xxcstallhist.xxcstall_sratecstadj_lbr
          tt_xxcstallhist.xxcstall_scurcyccntcstadj_ovh   =  - totrqty * tt_xxcstallhist.xxcstall_sratecstadj_ovh
          tt_xxcstallhist.xxcstall_scurcyccntcstadj_bdn   =  - totrqty * tt_xxcstallhist.xxcstall_sratecstadj_bdn
          tt_xxcstallhist.xxcstall_scurcyccntcstadj_sub   =  - totrqty * tt_xxcstallhist.xxcstall_sratecstadj_sub
		  /*
          tt_xxcstallhist.xxcstall_scurcyccntcyccnt       =  - totrqty * tt_xxcstallhist.xxcstall_sratecyccnt    
          tt_xxcstallhist.xxcstall_scurcyccntcyccnt_mtl   =  - totrqty * tt_xxcstallhist.xxcstall_sratecyccnt_mtl
          tt_xxcstallhist.xxcstall_scurcyccntcyccnt_lbr   =  - totrqty * tt_xxcstallhist.xxcstall_sratecyccnt_lbr
          tt_xxcstallhist.xxcstall_scurcyccntcyccnt_bdn   =  - totrqty * tt_xxcstallhist.xxcstall_sratecyccnt_bdn
          tt_xxcstallhist.xxcstall_scurcyccntcyccnt_ovh   =  - totrqty * tt_xxcstallhist.xxcstall_sratecyccnt_ovh
          tt_xxcstallhist.xxcstall_scurcyccntcyccnt_sub   =  - totrqty * tt_xxcstallhist.xxcstall_sratecyccnt_sub
          */
		  tt_xxcstallhist.xxcstall_scurcyccntcommon       =  - totrqty * tt_xxcstallhist.xxcstall_sratecommon    
          tt_xxcstallhist.xxcstall_scurcyccntothers       =  - totrqty * tt_xxcstallhist.xxcstall_srateothers   
          /*		  
          tt_xxcstallhist.xxcstall_scurcyccntraw          =  - totrqty * tt_xxcstallhist.xxcstall_srateraw       
          tt_xxcstallhist.xxcstall_scurcyccntsf           =  - totrqty * tt_xxcstallhist.xxcstall_sratesf        
          tt_xxcstallhist.xxcstall_scurcyccntsf_mtl       =  - totrqty * tt_xxcstallhist.xxcstall_sratesf_mtl    
          tt_xxcstallhist.xxcstall_scurcyccntsf_lbr       =  - totrqty * tt_xxcstallhist.xxcstall_sratesf_lbr    
          tt_xxcstallhist.xxcstall_scurcyccntsf_bdn       =  - totrqty * tt_xxcstallhist.xxcstall_sratesf_bdn    
          tt_xxcstallhist.xxcstall_scurcyccntsf_ovh       =  - totrqty * tt_xxcstallhist.xxcstall_sratesf_ovh    
          tt_xxcstallhist.xxcstall_scurcyccntsf_sub       =  - totrqty * tt_xxcstallhist.xxcstall_sratesf_sub 
          */
          .
		 /*** Manufacture variance allocation for ISS-SO ***/  
		 totrqty= 0.
         Run GetTrQty(input tt_xxcstallhist.xxcstall_part,
	                    input "ISS-SO",
	     				output totrqty).		  
          assign
		  tt_xxcstallhist.xxcstall_scurisssomusage        =  - totrqty * tt_xxcstallhist.xxcstall_sratemusage     
		  /*                                                                                              
          tt_xxcstallhist.xxcstall_scurisssomusage_mtl    =  - totrqty * tt_xxcstallhist.xxcstall_sratemusage_mtl 
          tt_xxcstallhist.xxcstall_scurisssomusage_lbr    =  - totrqty * tt_xxcstallhist.xxcstall_sratemusage_lbr 
          tt_xxcstallhist.xxcstall_scurisssomusage_bdn    =  - totrqty * tt_xxcstallhist.xxcstall_sratemusage_bdn 
          tt_xxcstallhist.xxcstall_scurisssomusage_ovh    =  - totrqty * tt_xxcstallhist.xxcstall_sratemusage_ovh 
          tt_xxcstallhist.xxcstall_scurisssomusage_sub    =  - totrqty * tt_xxcstallhist.xxcstall_sratemusage_sub 
		  */                                                                                              
          tt_xxcstallhist.xxcstall_scurisssomrate         =  - totrqty * tt_xxcstallhist.xxcstall_sratemrate      
          tt_xxcstallhist.xxcstall_scurisssommthd         =  - totrqty * tt_xxcstallhist.xxcstall_sratemmthd      
          tt_xxcstallhist.xxcstall_scurisssommthd_mtl     =  - totrqty * tt_xxcstallhist.xxcstall_sratemmthd_mtl  
          tt_xxcstallhist.xxcstall_scurisssommthd_lbr     =  - totrqty * tt_xxcstallhist.xxcstall_sratemmthd_lbr  
          tt_xxcstallhist.xxcstall_scurisssommthd_bdn     =  - totrqty * tt_xxcstallhist.xxcstall_sratemmthd_bdn  
          tt_xxcstallhist.xxcstall_scurisssommthd_ovh     =  - totrqty * tt_xxcstallhist.xxcstall_sratemmthd_ovh  
          tt_xxcstallhist.xxcstall_scurisssommthd_sub     =  - totrqty * tt_xxcstallhist.xxcstall_sratemmthd_sub  
          tt_xxcstallhist.xxcstall_scurisssosubusage      =  - totrqty * tt_xxcstallhist.xxcstall_sratesubusage   
          tt_xxcstallhist.xxcstall_scurisssosubrate       =  - totrqty * tt_xxcstallhist.xxcstall_sratesubrate    
          tt_xxcstallhist.xxcstall_scurisssolbrusage      =  - totrqty * tt_xxcstallhist.xxcstall_sratelbrusage   
          tt_xxcstallhist.xxcstall_scurisssolbrrate       =  - totrqty * tt_xxcstallhist.xxcstall_sratelbrrate    
          tt_xxcstallhist.xxcstall_scurisssobdnusage      =  - totrqty * tt_xxcstallhist.xxcstall_sratebdnusage   
          tt_xxcstallhist.xxcstall_scurisssobdnrate       =  - totrqty * tt_xxcstallhist.xxcstall_sratebdnrate    
          tt_xxcstallhist.xxcstall_scurisssocstadj        =  - totrqty * tt_xxcstallhist.xxcstall_sratecstadj     
          tt_xxcstallhist.xxcstall_scurisssocstadj_mtl    =  - totrqty * tt_xxcstallhist.xxcstall_sratecstadj_mtl 
          tt_xxcstallhist.xxcstall_scurisssocstadj_lbr    =  - totrqty * tt_xxcstallhist.xxcstall_sratecstadj_lbr 
          tt_xxcstallhist.xxcstall_scurisssocstadj_ovh    =  - totrqty * tt_xxcstallhist.xxcstall_sratecstadj_ovh 
          tt_xxcstallhist.xxcstall_scurisssocstadj_bdn    =  - totrqty * tt_xxcstallhist.xxcstall_sratecstadj_bdn 
          tt_xxcstallhist.xxcstall_scurisssocstadj_sub    =  - totrqty * tt_xxcstallhist.xxcstall_sratecstadj_sub 
          tt_xxcstallhist.xxcstall_scurisssoothers        =  - totrqty * tt_xxcstallhist.xxcstall_srateothers     
		                                                                                                
          tt_xxcstallhist.xxcstall_scurisssocommon        =  - totrqty * tt_xxcstallhist.xxcstall_sratecommon     
          /*
		  tt_xxcstallhist.xxcstall_scurisssocyccnt        =  - totrqty * tt_xxcstallhist.xxcstall_sratecyccnt     
          tt_xxcstallhist.xxcstall_scurisssocyccnt_mtl    =  - totrqty * tt_xxcstallhist.xxcstall_sratecyccnt_mtl 
          tt_xxcstallhist.xxcstall_scurisssocyccnt_lbr    =  - totrqty * tt_xxcstallhist.xxcstall_sratecyccnt_lbr 
          tt_xxcstallhist.xxcstall_scurisssocyccnt_bdn    =  - totrqty * tt_xxcstallhist.xxcstall_sratecyccnt_bdn 
          tt_xxcstallhist.xxcstall_scurisssocyccnt_ovh    =  - totrqty * tt_xxcstallhist.xxcstall_sratecyccnt_ovh 
          tt_xxcstallhist.xxcstall_scurisssocyccnt_sub    =  - totrqty * tt_xxcstallhist.xxcstall_sratecyccnt_sub 
          tt_xxcstallhist.xxcstall_scurisssoraw           =  - totrqty * tt_xxcstallhist.xxcstall_srateraw        
          tt_xxcstallhist.xxcstall_scurisssosf            =  - totrqty * tt_xxcstallhist.xxcstall_sratesf         
          tt_xxcstallhist.xxcstall_scurisssosf_mtl        =  - totrqty * tt_xxcstallhist.xxcstall_sratesf_mtl     
          tt_xxcstallhist.xxcstall_scurisssosf_lbr        =  - totrqty * tt_xxcstallhist.xxcstall_sratesf_lbr     
          tt_xxcstallhist.xxcstall_scurisssosf_bdn        =  - totrqty * tt_xxcstallhist.xxcstall_sratesf_bdn     
          tt_xxcstallhist.xxcstall_scurisssosf_ovh        =  - totrqty * tt_xxcstallhist.xxcstall_sratesf_ovh     
          tt_xxcstallhist.xxcstall_scurisssosf_sub        =  - totrqty * tt_xxcstallhist.xxcstall_sratesf_sub     
          */
		  
		  .
	 
  
	 end.
	 
	 /* roll-up iss-wo variance */
	 /*
	 message "Roll-up" view-as alert-box.
	 */
	 {us/xx/xxcstwosumup_e.i entity site li_year li_period}
	 /** calculate end variance **/
	 
	 for each tt_xxcstallhist.
     tt_xxcstallhist.xxcstall_endppv          = tt_xxcstallhist.xxcstall_begppv        +    tt_xxcstallhist.xxcstall_curppv        -           tt_xxcstallhist.xxcstall_curissunpppv        -        tt_xxcstallhist.xxcstall_curisswoppv        -          tt_xxcstallhist.xxcstall_curisssoppv        -     tt_xxcstallhist.xxcstall_curcyccntppv        .
     tt_xxcstallhist.xxcstall_endfreight      = tt_xxcstallhist.xxcstall_begfreight    +    tt_xxcstallhist.xxcstall_curfreight    -           tt_xxcstallhist.xxcstall_curissunpfreight    -        tt_xxcstallhist.xxcstall_curisswofreight    -          tt_xxcstallhist.xxcstall_curisssofreight    -     tt_xxcstallhist.xxcstall_curcyccntfreight    .
     tt_xxcstallhist.xxcstall_endapusage      = tt_xxcstallhist.xxcstall_begapusage    +    tt_xxcstallhist.xxcstall_curapusage    -           tt_xxcstallhist.xxcstall_curissunpapusage    -        tt_xxcstallhist.xxcstall_curisswoapusage    -          tt_xxcstallhist.xxcstall_curisssoapusage    -     tt_xxcstallhist.xxcstall_curcyccntapusage    .
     tt_xxcstallhist.xxcstall_endaprate       = tt_xxcstallhist.xxcstall_begaprate     +    tt_xxcstallhist.xxcstall_curaprate     -           tt_xxcstallhist.xxcstall_curissunpaprate     -        tt_xxcstallhist.xxcstall_curisswoaprate     -          tt_xxcstallhist.xxcstall_curisssoaprate     -     tt_xxcstallhist.xxcstall_curcyccntaprate     .
     tt_xxcstallhist.xxcstall_endcstadj       = tt_xxcstallhist.xxcstall_begcstadj     +    tt_xxcstallhist.xxcstall_curcstadj     -           tt_xxcstallhist.xxcstall_curissunpcstadj     -        tt_xxcstallhist.xxcstall_curisswocstadj     -          tt_xxcstallhist.xxcstall_curisssocstadj     -     tt_xxcstallhist.xxcstall_curcyccntcstadj     .
     tt_xxcstallhist.xxcstall_endcstadj_mtl   = tt_xxcstallhist.xxcstall_begcstadj_mtl +    tt_xxcstallhist.xxcstall_curcstadj_mtl -           tt_xxcstallhist.xxcstall_curissunpcstadj_mtl -        tt_xxcstallhist.xxcstall_curisswocstadj_mtl -          tt_xxcstallhist.xxcstall_curisssocstadj_mtl -     tt_xxcstallhist.xxcstall_curcyccntcstadj_mtl . 
     tt_xxcstallhist.xxcstall_endcstadj_lbr   = tt_xxcstallhist.xxcstall_begcstadj_lbr +    tt_xxcstallhist.xxcstall_curcstadj_lbr -           tt_xxcstallhist.xxcstall_curissunpcstadj_lbr -        tt_xxcstallhist.xxcstall_curisswocstadj_lbr -          tt_xxcstallhist.xxcstall_curisssocstadj_lbr -     tt_xxcstallhist.xxcstall_curcyccntcstadj_lbr . 
     tt_xxcstallhist.xxcstall_endcstadj_bdn   = tt_xxcstallhist.xxcstall_begcstadj_bdn +    tt_xxcstallhist.xxcstall_curcstadj_bdn -           tt_xxcstallhist.xxcstall_curissunpcstadj_bdn -        tt_xxcstallhist.xxcstall_curisswocstadj_bdn -          tt_xxcstallhist.xxcstall_curisssocstadj_ovh -     tt_xxcstallhist.xxcstall_curcyccntcstadj_bdn . 
     tt_xxcstallhist.xxcstall_endcstadj_ovh   = tt_xxcstallhist.xxcstall_begcstadj_ovh +    tt_xxcstallhist.xxcstall_curcstadj_ovh -           tt_xxcstallhist.xxcstall_curissunpcstadj_ovh -        tt_xxcstallhist.xxcstall_curisswocstadj_ovh -          tt_xxcstallhist.xxcstall_curisssocstadj_bdn -     tt_xxcstallhist.xxcstall_curcyccntcstadj_ovh . 
     tt_xxcstallhist.xxcstall_endcstadj_sub   = tt_xxcstallhist.xxcstall_begcstadj_sub +    tt_xxcstallhist.xxcstall_curcstadj_sub -           tt_xxcstallhist.xxcstall_curissunpcstadj_sub -        tt_xxcstallhist.xxcstall_curisswocstadj_sub -          tt_xxcstallhist.xxcstall_curisssocstadj_sub -     tt_xxcstallhist.xxcstall_curcyccntcstadj_sub . 
     tt_xxcstallhist.xxcstall_endcyccnt       = tt_xxcstallhist.xxcstall_begcyccnt     +    tt_xxcstallhist.xxcstall_curcyccnt     -           tt_xxcstallhist.xxcstall_curissunpcyccnt     -        tt_xxcstallhist.xxcstall_curisswocyccnt     -          tt_xxcstallhist.xxcstall_curisssocyccnt     -     tt_xxcstallhist.xxcstall_curcyccntcyccnt     .
     tt_xxcstallhist.xxcstall_endcyccnt_mtl   = tt_xxcstallhist.xxcstall_begcyccnt_mtl +    tt_xxcstallhist.xxcstall_curcyccnt_mtl -           tt_xxcstallhist.xxcstall_curissunpcyccnt_mtl -        tt_xxcstallhist.xxcstall_curisswocyccnt_mtl -          tt_xxcstallhist.xxcstall_curisssocyccnt_mtl -     tt_xxcstallhist.xxcstall_curcyccntcyccnt_mtl . 
     tt_xxcstallhist.xxcstall_endcyccnt_lbr   = tt_xxcstallhist.xxcstall_begcyccnt_lbr +    tt_xxcstallhist.xxcstall_curcyccnt_lbr -           tt_xxcstallhist.xxcstall_curissunpcyccnt_lbr -        tt_xxcstallhist.xxcstall_curisswocyccnt_lbr -          tt_xxcstallhist.xxcstall_curisssocyccnt_lbr -     tt_xxcstallhist.xxcstall_curcyccntcyccnt_lbr . 
     tt_xxcstallhist.xxcstall_endcyccnt_bdn   = tt_xxcstallhist.xxcstall_begcyccnt_bdn +    tt_xxcstallhist.xxcstall_curcyccnt_bdn -           tt_xxcstallhist.xxcstall_curissunpcyccnt_bdn -        tt_xxcstallhist.xxcstall_curisswocyccnt_bdn -          tt_xxcstallhist.xxcstall_curisssocyccnt_bdn -     tt_xxcstallhist.xxcstall_curcyccntcyccnt_bdn . 
     tt_xxcstallhist.xxcstall_endcyccnt_ovh   = tt_xxcstallhist.xxcstall_begcyccnt_ovh +    tt_xxcstallhist.xxcstall_curcyccnt_ovh -           tt_xxcstallhist.xxcstall_curissunpcyccnt_ovh -        tt_xxcstallhist.xxcstall_curisswocyccnt_ovh -          tt_xxcstallhist.xxcstall_curisssocyccnt_ovh -     tt_xxcstallhist.xxcstall_curcyccntcyccnt_ovh . 
     tt_xxcstallhist.xxcstall_endcyccnt_sub   = tt_xxcstallhist.xxcstall_begcyccnt_sub +    tt_xxcstallhist.xxcstall_curcyccnt_sub -           tt_xxcstallhist.xxcstall_curissunpcyccnt_sub -        tt_xxcstallhist.xxcstall_curisswocyccnt_sub -          tt_xxcstallhist.xxcstall_curisssocyccnt_sub -     tt_xxcstallhist.xxcstall_curcyccntcyccnt_sub . 
     tt_xxcstallhist.xxcstall_endcommon       = tt_xxcstallhist.xxcstall_begcommon     +    tt_xxcstallhist.xxcstall_curcommon     -           tt_xxcstallhist.xxcstall_curissunpcommon     -        tt_xxcstallhist.xxcstall_curisswocommon     -          tt_xxcstallhist.xxcstall_curisssocommon     -     tt_xxcstallhist.xxcstall_curcyccntcommon     .
     tt_xxcstallhist.xxcstall_endothers       = tt_xxcstallhist.xxcstall_begothers     +    tt_xxcstallhist.xxcstall_curothers     -           tt_xxcstallhist.xxcstall_curissunpothers     -        tt_xxcstallhist.xxcstall_curisswoothers     -          tt_xxcstallhist.xxcstall_curisssoothers     -     tt_xxcstallhist.xxcstall_curcyccntothers     .
	 tt_xxcstallhist.xxcstall_sendmusage      = tt_xxcstallhist.xxcstall_sbegmusage       + tt_xxcstallhist.xxcstall_scurmusage      -         tt_xxcstallhist.xxcstall_scurissunpmusage      -      tt_xxcstallhist.xxcstall_scurisswomusage       -       tt_xxcstallhist.xxcstall_scurcyccntmusage     -   tt_xxcstallhist.xxcstall_scurisssomusage     .
	 tt_xxcstallhist.xxcstall_sendmusage_mtl  = tt_xxcstallhist.xxcstall_sbegmrate        + tt_xxcstallhist.xxcstall_scurmusage_mtl  -         tt_xxcstallhist.xxcstall_scurissunpmusage_mtl  -      tt_xxcstallhist.xxcstall_scurisswomusage_mtl   -       tt_xxcstallhist.xxcstall_scurcyccntmusage_mtl -   tt_xxcstallhist.xxcstall_scurisssomusage_mtl .
	 tt_xxcstallhist.xxcstall_sendmusage_lbr  = tt_xxcstallhist.xxcstall_sbegmmthd        + tt_xxcstallhist.xxcstall_scurmusage_lbr  -         tt_xxcstallhist.xxcstall_scurissunpmusage_lbr  -      tt_xxcstallhist.xxcstall_scurisswomusage_lbr   -       tt_xxcstallhist.xxcstall_scurcyccntmusage_lbr -   tt_xxcstallhist.xxcstall_scurisssomusage_lbr .
	 tt_xxcstallhist.xxcstall_sendmusage_bdn  = tt_xxcstallhist.xxcstall_sbegmmthd_mtl    + tt_xxcstallhist.xxcstall_scurmusage_bdn  -         tt_xxcstallhist.xxcstall_scurissunpmusage_bdn  -      tt_xxcstallhist.xxcstall_scurisswomusage_bdn   -       tt_xxcstallhist.xxcstall_scurcyccntmusage_bdn -   tt_xxcstallhist.xxcstall_scurisssomusage_bdn .
	 tt_xxcstallhist.xxcstall_sendmusage_ovh  = tt_xxcstallhist.xxcstall_sbegmmthd_lbr    + tt_xxcstallhist.xxcstall_scurmusage_ovh  -         tt_xxcstallhist.xxcstall_scurissunpmusage_ovh  -      tt_xxcstallhist.xxcstall_scurisswomusage_ovh   -       tt_xxcstallhist.xxcstall_scurcyccntmusage_ovh -   tt_xxcstallhist.xxcstall_scurisssomusage_ovh .
	 tt_xxcstallhist.xxcstall_sendmusage_sub  = tt_xxcstallhist.xxcstall_sbegmmthd_bdn    + tt_xxcstallhist.xxcstall_scurmusage_sub  -         tt_xxcstallhist.xxcstall_scurissunpmusage_sub  -      tt_xxcstallhist.xxcstall_scurisswomusage_sub   -       tt_xxcstallhist.xxcstall_scurcyccntmusage_sub -   tt_xxcstallhist.xxcstall_scurisssomusage_sub .
	 tt_xxcstallhist.xxcstall_sendmrate       = tt_xxcstallhist.xxcstall_sbegmmthd_ovh    + tt_xxcstallhist.xxcstall_scurmrate       -         tt_xxcstallhist.xxcstall_scurissunpmrate       -      tt_xxcstallhist.xxcstall_scurisswomrate        -       tt_xxcstallhist.xxcstall_scurcyccntmrate      -   tt_xxcstallhist.xxcstall_scurisssomrate      .
	 tt_xxcstallhist.xxcstall_sendmmthd       = tt_xxcstallhist.xxcstall_sbegmmthd_sub    + tt_xxcstallhist.xxcstall_scurmmthd       -         tt_xxcstallhist.xxcstall_scurissunpmmthd       -      tt_xxcstallhist.xxcstall_scurisswommthd        -       tt_xxcstallhist.xxcstall_scurcyccntmmthd      -   tt_xxcstallhist.xxcstall_scurisssommthd      .
	 tt_xxcstallhist.xxcstall_sendmmthd_mtl   = tt_xxcstallhist.xxcstall_sbegsubusage     + tt_xxcstallhist.xxcstall_scurmmthd_mtl   -         tt_xxcstallhist.xxcstall_scurissunpmmthd_mtl   -      tt_xxcstallhist.xxcstall_scurisswommthd_mtl    -       tt_xxcstallhist.xxcstall_scurcyccntmmthd_mtl  -   tt_xxcstallhist.xxcstall_scurisssommthd_mtl  .
	 tt_xxcstallhist.xxcstall_sendmmthd_lbr   = tt_xxcstallhist.xxcstall_sbegsubusage_mtl + tt_xxcstallhist.xxcstall_scurmmthd_lbr   -         tt_xxcstallhist.xxcstall_scurissunpmmthd_lbr   -      tt_xxcstallhist.xxcstall_scurisswommthd_lbr    -       tt_xxcstallhist.xxcstall_scurcyccntmmthd_lbr  -   tt_xxcstallhist.xxcstall_scurisssommthd_lbr  .
	 tt_xxcstallhist.xxcstall_sendmmthd_bdn   = tt_xxcstallhist.xxcstall_sbegsubusage_lbr + tt_xxcstallhist.xxcstall_scurmmthd_bdn   -         tt_xxcstallhist.xxcstall_scurissunpmmthd_bdn   -      tt_xxcstallhist.xxcstall_scurisswommthd_bdn    -       tt_xxcstallhist.xxcstall_scurcyccntmmthd_bdn  -   tt_xxcstallhist.xxcstall_scurisssommthd_bdn  .
	 tt_xxcstallhist.xxcstall_sendmmthd_ovh   = tt_xxcstallhist.xxcstall_sbegsubusage_bdn + tt_xxcstallhist.xxcstall_scurmmthd_ovh   -         tt_xxcstallhist.xxcstall_scurissunpmmthd_ovh   -      tt_xxcstallhist.xxcstall_scurisswommthd_ovh    -       tt_xxcstallhist.xxcstall_scurcyccntmmthd_ovh  -   tt_xxcstallhist.xxcstall_scurisssommthd_ovh  .
	 tt_xxcstallhist.xxcstall_sendmmthd_sub   = tt_xxcstallhist.xxcstall_sbegsubusage_ovh + tt_xxcstallhist.xxcstall_scurmmthd_sub   -         tt_xxcstallhist.xxcstall_scurissunpmmthd_sub   -      tt_xxcstallhist.xxcstall_scurisswommthd_sub    -       tt_xxcstallhist.xxcstall_scurcyccntmmthd_sub  -   tt_xxcstallhist.xxcstall_scurisssommthd_sub  .
	 tt_xxcstallhist.xxcstall_sendsubusage    = tt_xxcstallhist.xxcstall_sbegsubusage_sub + tt_xxcstallhist.xxcstall_scursubusage    -         tt_xxcstallhist.xxcstall_scurissunpsubusage    -      tt_xxcstallhist.xxcstall_scurisswosubusage     -       tt_xxcstallhist.xxcstall_scurcyccntsubusage   -   tt_xxcstallhist.xxcstall_scurisssosubusage   .
	 tt_xxcstallhist.xxcstall_sendsubrate     = tt_xxcstallhist.xxcstall_sbegsubrate      + tt_xxcstallhist.xxcstall_scursubrate     -         tt_xxcstallhist.xxcstall_scurissunpsubrate     -      tt_xxcstallhist.xxcstall_scurisswosubrate      -       tt_xxcstallhist.xxcstall_scurcyccntsubrate    -   tt_xxcstallhist.xxcstall_scurisssosubrate    .
	 tt_xxcstallhist.xxcstall_sendlbrusage    = tt_xxcstallhist.xxcstall_sbeglbrusage     + tt_xxcstallhist.xxcstall_scurlbrusage    -         tt_xxcstallhist.xxcstall_scurissunplbrusage    -      tt_xxcstallhist.xxcstall_scurisswolbrusage     -       tt_xxcstallhist.xxcstall_scurcyccntlbrusage   -   tt_xxcstallhist.xxcstall_scurisssolbrusage   .
	 tt_xxcstallhist.xxcstall_sendlbrrate     = tt_xxcstallhist.xxcstall_sbeglbrrate      + tt_xxcstallhist.xxcstall_scurlbrrate     -         tt_xxcstallhist.xxcstall_scurissunplbrrate     -      tt_xxcstallhist.xxcstall_scurisswolbrrate      -       tt_xxcstallhist.xxcstall_scurcyccntlbrrate    -   tt_xxcstallhist.xxcstall_scurisssolbrrate    .
	 tt_xxcstallhist.xxcstall_sendbdnusage    = tt_xxcstallhist.xxcstall_sbegbdnusage     + tt_xxcstallhist.xxcstall_scurbdnusage    -         tt_xxcstallhist.xxcstall_scurissunpbdnusage    -      tt_xxcstallhist.xxcstall_scurisswobdnusage     -       tt_xxcstallhist.xxcstall_scurcyccntbdnusage   -   tt_xxcstallhist.xxcstall_scurisssobdnusage   .
	 tt_xxcstallhist.xxcstall_sendbdnrate     = tt_xxcstallhist.xxcstall_sbegbdnrate      + tt_xxcstallhist.xxcstall_scurbdnrate     -         tt_xxcstallhist.xxcstall_scurissunpbdnrate     -      tt_xxcstallhist.xxcstall_scurisswobdnrate      -       tt_xxcstallhist.xxcstall_scurcyccntbdnrate    -   tt_xxcstallhist.xxcstall_scurisssobdnrate    .
	 tt_xxcstallhist.xxcstall_sendcstadj      = tt_xxcstallhist.xxcstall_sbegcstadj       + tt_xxcstallhist.xxcstall_scurcstadj      -         tt_xxcstallhist.xxcstall_scurissunpcstadj      -      tt_xxcstallhist.xxcstall_scurisswocstadj       -       tt_xxcstallhist.xxcstall_scurcyccntcstadj     -   tt_xxcstallhist.xxcstall_scurisssocstadj     .
	 tt_xxcstallhist.xxcstall_sendcstadj_mtl  = tt_xxcstallhist.xxcstall_sbegcstadj_mtl   + tt_xxcstallhist.xxcstall_scurcstadj_mtl  -         tt_xxcstallhist.xxcstall_scurissunpcstadj_mtl  -      tt_xxcstallhist.xxcstall_scurisswocstadj_mtl   -       tt_xxcstallhist.xxcstall_scurcyccntcstadj_mtl -   tt_xxcstallhist.xxcstall_scurisssocstadj_mtl .
	 tt_xxcstallhist.xxcstall_sendcstadj_lbr  = tt_xxcstallhist.xxcstall_sbegcstadj_lbr   + tt_xxcstallhist.xxcstall_scurcstadj_lbr  -         tt_xxcstallhist.xxcstall_scurissunpcstadj_lbr  -      tt_xxcstallhist.xxcstall_scurisswocstadj_lbr   -       tt_xxcstallhist.xxcstall_scurcyccntcstadj_lbr -   tt_xxcstallhist.xxcstall_scurisssocstadj_lbr .
	 tt_xxcstallhist.xxcstall_sendcstadj_bdn  = tt_xxcstallhist.xxcstall_sbegcstadj_bdn   + tt_xxcstallhist.xxcstall_scurcstadj_bdn  -         tt_xxcstallhist.xxcstall_scurissunpcstadj_ovh  -      tt_xxcstallhist.xxcstall_scurisswocstadj_ovh   -       tt_xxcstallhist.xxcstall_scurcyccntcstadj_ovh -   tt_xxcstallhist.xxcstall_scurisssocstadj_ovh .
	 tt_xxcstallhist.xxcstall_sendcstadj_ovh  = tt_xxcstallhist.xxcstall_sbegcstadj_ovh   + tt_xxcstallhist.xxcstall_scurcstadj_ovh  -         tt_xxcstallhist.xxcstall_scurissunpcstadj_bdn  -      tt_xxcstallhist.xxcstall_scurisswocstadj_bdn   -       tt_xxcstallhist.xxcstall_scurcyccntcstadj_bdn -   tt_xxcstallhist.xxcstall_scurisssocstadj_bdn .
	 tt_xxcstallhist.xxcstall_sendcstadj_sub  = tt_xxcstallhist.xxcstall_sbegcstadj_sub   + tt_xxcstallhist.xxcstall_scurcstadj_sub  -         tt_xxcstallhist.xxcstall_scurissunpcstadj_sub  -      tt_xxcstallhist.xxcstall_scurisswocstadj_sub   -       tt_xxcstallhist.xxcstall_scurcyccntcstadj_sub -   tt_xxcstallhist.xxcstall_scurisssocstadj_sub .
	 tt_xxcstallhist.xxcstall_sendcyccnt      = tt_xxcstallhist.xxcstall_sbegcyccnt       + tt_xxcstallhist.xxcstall_scurcyccnt      -         tt_xxcstallhist.xxcstall_scurissunpcyccnt      -      tt_xxcstallhist.xxcstall_scurisswocyccnt       -       tt_xxcstallhist.xxcstall_scurcyccntcyccnt     -   tt_xxcstallhist.xxcstall_scurisssoothers     .
	 tt_xxcstallhist.xxcstall_sendcyccnt_mtl  = tt_xxcstallhist.xxcstall_sbegcyccnt_mtl   + tt_xxcstallhist.xxcstall_scurcyccnt_mtl  -         tt_xxcstallhist.xxcstall_scurissunpcyccnt_mtl  -      tt_xxcstallhist.xxcstall_scurisswocyccnt_mtl   -       tt_xxcstallhist.xxcstall_scurcyccntcyccnt_mtl -   tt_xxcstallhist.xxcstall_scurisssocommon     .
	 tt_xxcstallhist.xxcstall_sendcyccnt_lbr  = tt_xxcstallhist.xxcstall_sbegcyccnt_lbr   + tt_xxcstallhist.xxcstall_scurcyccnt_lbr  -         tt_xxcstallhist.xxcstall_scurissunpcyccnt_lbr  -      tt_xxcstallhist.xxcstall_scurisswocyccnt_lbr   -       tt_xxcstallhist.xxcstall_scurcyccntcyccnt_lbr -   tt_xxcstallhist.xxcstall_scurisssocyccnt     .
	 tt_xxcstallhist.xxcstall_sendcyccnt_bdn  = tt_xxcstallhist.xxcstall_sbegcyccnt_bdn   + tt_xxcstallhist.xxcstall_scurcyccnt_bdn  -         tt_xxcstallhist.xxcstall_scurissunpcyccnt_bdn  -      tt_xxcstallhist.xxcstall_scurisswocyccnt_bdn   -       tt_xxcstallhist.xxcstall_scurcyccntcyccnt_bdn -   tt_xxcstallhist.xxcstall_scurisssocyccnt_mtl .
	 tt_xxcstallhist.xxcstall_sendcyccnt_ovh  = tt_xxcstallhist.xxcstall_sbegcyccnt_ovh   + tt_xxcstallhist.xxcstall_scurcyccnt_ovh  -         tt_xxcstallhist.xxcstall_scurissunpcyccnt_ovh  -      tt_xxcstallhist.xxcstall_scurisswocyccnt_ovh   -       tt_xxcstallhist.xxcstall_scurcyccntcyccnt_ovh -   tt_xxcstallhist.xxcstall_scurisssocyccnt_lbr .
	 tt_xxcstallhist.xxcstall_sendcyccnt_sub  = tt_xxcstallhist.xxcstall_sbegcyccnt_sub   + tt_xxcstallhist.xxcstall_scurcyccnt_sub  -         tt_xxcstallhist.xxcstall_scurissunpcyccnt_sub  -      tt_xxcstallhist.xxcstall_scurisswocyccnt_sub   -       tt_xxcstallhist.xxcstall_scurcyccntcyccnt_sub -   tt_xxcstallhist.xxcstall_scurisssocyccnt_bdn .
	 tt_xxcstallhist.xxcstall_sendcommon      = tt_xxcstallhist.xxcstall_sbegcommon       + tt_xxcstallhist.xxcstall_scurcommon      -         tt_xxcstallhist.xxcstall_scurissunpcommon      -      tt_xxcstallhist.xxcstall_scurisswocommon       -       tt_xxcstallhist.xxcstall_scurcyccntcommon     -   tt_xxcstallhist.xxcstall_scurisssocyccnt_ovh .
	 tt_xxcstallhist.xxcstall_sendothers      = tt_xxcstallhist.xxcstall_sbegothers       + tt_xxcstallhist.xxcstall_scurothers      -         tt_xxcstallhist.xxcstall_scurissunpothers      -      tt_xxcstallhist.xxcstall_scurisswoothers       -       tt_xxcstallhist.xxcstall_scurcyccntothers     -   tt_xxcstallhist.xxcstall_scurisssocyccnt_sub .
	 tt_xxcstallhist.xxcstall_sendraw         = tt_xxcstallhist.xxcstall_sbegraw          + tt_xxcstallhist.xxcstall_scurraw         -         tt_xxcstallhist.xxcstall_scurissunpraw         -      tt_xxcstallhist.xxcstall_scurissworaw          -       tt_xxcstallhist.xxcstall_scurcyccntraw        -   tt_xxcstallhist.xxcstall_scurisssoraw        .
	 tt_xxcstallhist.xxcstall_sendsf          = tt_xxcstallhist.xxcstall_sbegsf           + tt_xxcstallhist.xxcstall_scursf          -         tt_xxcstallhist.xxcstall_scurissunpsf          -      tt_xxcstallhist.xxcstall_scurisswosf           -       tt_xxcstallhist.xxcstall_scurcyccntsf         -   tt_xxcstallhist.xxcstall_scurisssosf         .
	 tt_xxcstallhist.xxcstall_sendsf_mtl      = tt_xxcstallhist.xxcstall_sbegsf_mtl       + tt_xxcstallhist.xxcstall_scursf_mtl      -         tt_xxcstallhist.xxcstall_scurissunpsf_mtl      -      tt_xxcstallhist.xxcstall_scurisswosf_mtl       -       tt_xxcstallhist.xxcstall_scurcyccntsf_mtl     -   tt_xxcstallhist.xxcstall_scurisssosf_mtl     .
	 tt_xxcstallhist.xxcstall_sendsf_lbr      = tt_xxcstallhist.xxcstall_sbegsf_lbr       + tt_xxcstallhist.xxcstall_scursf_lbr      -         tt_xxcstallhist.xxcstall_scurissunpsf_lbr      -      tt_xxcstallhist.xxcstall_scurisswosf_lbr       -       tt_xxcstallhist.xxcstall_scurcyccntsf_lbr     -   tt_xxcstallhist.xxcstall_scurisssosf_lbr     .
	 tt_xxcstallhist.xxcstall_sendsf_bdn      = tt_xxcstallhist.xxcstall_sbegsf_bdn       + tt_xxcstallhist.xxcstall_scursf_bdn      -         tt_xxcstallhist.xxcstall_scurissunpsf_bdn      -      tt_xxcstallhist.xxcstall_scurisswosf_bdn       -       tt_xxcstallhist.xxcstall_scurcyccntsf_bdn     -   tt_xxcstallhist.xxcstall_scurisssosf_bdn     .
	 tt_xxcstallhist.xxcstall_sendsf_ovh      = tt_xxcstallhist.xxcstall_sbegsf_ovh       + tt_xxcstallhist.xxcstall_scursf_ovh      -         tt_xxcstallhist.xxcstall_scurissunpsf_ovh      -      tt_xxcstallhist.xxcstall_scurisswosf_ovh       -       tt_xxcstallhist.xxcstall_scurcyccntsf_ovh     -   tt_xxcstallhist.xxcstall_scurisssosf_ovh     .
	 tt_xxcstallhist.xxcstall_sendsf_sub      = tt_xxcstallhist.xxcstall_sbegsf_sub       + tt_xxcstallhist.xxcstall_scursf_sub      -         tt_xxcstallhist.xxcstall_scurissunpsf_sub      -      tt_xxcstallhist.xxcstall_scurisswosf_sub       -       tt_xxcstallhist.xxcstall_scurcyccntsf_sub     -   tt_xxcstallhist.xxcstall_scurisssosf_sub     .
	  
	 
	 end.
	 
	 
	 
	 /* save result to xxcstall_hist and xxcstwoallhist_hist */
	 if  li_update then do:
	 
	 for each tt_xxcstallhist .
	   create xxcstall_hist.
	   buffer-copy tt_xxcstallhist to xxcstall_hist.
	 end.
	 
	 for each tt_xxcstwoallhist.
	   create xxcstwoallhist_hist.
	   buffer-copy tt_xxcstwoallhist to xxcstwoallhist_hist.
	 end.
	 
	 
	 /* bug found for not select site and year period */
	 find first xxcstctrl_ctrl  where xxcstctrl_ctrl.xxcstctrl_domain = global_domain 
                           and xxcstctrl_ctrl.xxcstctrl_entity = entity 
			               and xxcstctrl_ctrl.xxcstctrl_site = site 
                           and xxcstctrl_ctrl.xxcstctrl_ID  = ""  no-error  .
         xxcstctrl_ctrl.xxcstctrl_per = xxcstctrl_ctrl.xxcstctrl_per + 1.
         if xxcstctrl_ctrl.xxcstctrl_per = 13 then 
         assign xxcstctrl_ctrl.xxcstctrl_per = 1
                xxcstctrl_ctrl.xxcstctrl_year = xxcstctrl_ctrl.xxcstctrl_year + 1.

	 end.

   end. /* if available xxcstctrl_ctrl record */
    {us/mf/mfselprt.i "printer" 132}	

    for each tt_xxcstallhist.
	export tt_xxcstallhist
    
         .
	end.
	for each tt_xxcstwoallhist.
	export tt_xxcstwoallhist.
	
	end.
     

	{us/mf/mfrtrail.i}
end.

PROCEDURE GetTotalRctQty.

DEFINE INPUT PARAMETER I_Part  like pt_part.
DEFINE INPUT PARAMETER I_Entity  like en_entity.
DEFINE INPUT PARAMETER I_Site  like in_site.
DEFINE INPUT PARAMETER I_year  like glc_year.
DEFINE INPUT PARAMETER I_period  like glc_per.
DEFINE OUTPUT PARAMETER O_TOTQTY like ld_qty_oh.

  O_TOTQTY = 0.
  for each xxcsttrvar_mstr no-lock where xxcsttrvar_mstr.xxcsttrvar_domain = global_domain
                                  and xxcsttrvar_mstr.xxcsttrvar_site   = I_site
 								 and xxcsttrvar_mstr.xxcsttrvar_part   =  I_part
 								 and xxcsttrvar_mstr.xxcsttrvar_year   =  I_year
 								 and xxcsttrvar_mstr.xxcsttrvar_period =  I_period
 								 and xxcsttrvar_mstr.xxcsttrvar_entity =  I_entity.
		if xxcsttrvar_mstr.xxcsttrvar_var_type = "rct-po" or 
		   xxcsttrvar_mstr.xxcsttrvar_var_type = "iss-prv" or
		   xxcsttrvar_mstr.xxcsttrvar_var_type = "rct-unp" or
		   xxcsttrvar_mstr.xxcsttrvar_var_type = "rct-wo" or
		   xxcsttrvar_mstr.xxcsttrvar_var_type = "invprofit" then 
		 assign O_TOTQTY = xxcsttrvar_mstr.xxcsttrvar_qty + O_TOTQTY.
   end.
   
END.

PROCEDURE GetTrQty.

DEFINE INPUT PARAMETER I_Part    like pt_part.
DEFINE INPUT PARAMETER I_vartype as character.
DEFINE OUTPUT PARAMETER O_TOTQTY like ld_qty_oh.

  O_TOTQTY = 0.
  for each xxcsttrvar_mstr no-lock where xxcsttrvar_mstr.xxcsttrvar_domain = global_domain
                                     and xxcsttrvar_mstr.xxcsttrvar_site   = site
 								     and xxcsttrvar_mstr.xxcsttrvar_part   =  I_part
 								     and xxcsttrvar_mstr.xxcsttrvar_year   =  li_year
 								     and xxcsttrvar_mstr.xxcsttrvar_period =  li_period
 								     and xxcsttrvar_mstr.xxcsttrvar_entity =   entity
								     and xxcsttrvar_mstr.xxcsttrvar_var_type = I_vartype.
		if xxcsttrvar_mstr.xxcsttrvar_var_type = i_vartype then 
		 assign O_TOTQTY = xxcsttrvar_mstr.xxcsttrvar_qty.
   end.
   
END.



PROCEDURE GetBegInvQty.

DEFINE INPUT PARAMETER I_Part  like pt_part.
DEFINE INPUT PARAMETER I_Entity  like en_entity.
DEFINE INPUT PARAMETER I_Site  like in_site.
DEFINE INPUT PARAMETER I_year  like glc_year.
DEFINE INPUT PARAMETER I_period  like glc_per.
DEFINE OUTPUT PARAMETER O_BEGQTY like ld_qty_oh.

  O_BEGQTY = 0.
  for each xxcsttrinvd_det no-lock where xxcsttrinvd_det.xxcsttrinvd_domain = global_domain
                                  and    xxcsttrinvd_det.xxcsttrinvd_site   = I_site
 								 and     xxcsttrinvd_det.xxcsttrinvd_part   =  I_part
 								 and     xxcsttrinvd_det.xxcsttrinvd_year   =  I_year
 								 and     xxcsttrinvd_det.xxcsttrinvd_period =  I_period
 								 and     xxcsttrinvd_det.xxcsttrinvd_entity =  I_entity.

		 assign O_BEGQTY = xxcsttrinvd_det.xxcsttrinvd_begin_inv + O_BEGQTY.
   end.
   
END.
PROCEDURE GetSameISSRCTWO.

DEFINE INPUT PARAMETER I_Part  like pt_part.
DEFINE INPUT PARAMETER I_Entity  like en_entity.
DEFINE INPUT PARAMETER I_Site  like in_site.
DEFINE INPUT PARAMETER I_year  like glc_year.
DEFINE INPUT PARAMETER I_period  like glc_per.
DEFINE OUTPUT PARAMETER O_ISSQTY like ld_qty_oh.
 O_ISSQTY = 0.
 for first xxcstissrctsum_mstr where xxcstissrctsum_mstr.xxcstissrctsum_domain = global_domain
                                and xxcstissrctsum_mstr.xxcstissrctsum_site   = i_site
                                and xxcstissrctsum_mstr.xxcstissrctsum_year   = i_year
                                and xxcstissrctsum_mstr.xxcstissrctsum_period = i_period
                                and xxcstissrctsum_mstr.xxcstissrctsum_entity = i_entity
								and xxcstissrctsum_mstr.xxcstissrctsum_comp  = I_part
                                and xxcstissrctsum_mstr.xxcstissrctsum_par =  xxcstissrctsum_mstr.xxcstissrctsum_comp
								no-lock .
 end.
 
 if available xxcstissrctsum_mstr then 
 O_ISSQTY = xxcstissrctsum_mstr.xxcstissrctsum_iss_totqty.


END.